简体   繁体   English

O(N + m)与O(NM)之间的复杂度计算差异

[英]Complexity computational difference between O(N+m) and O(NM)

In the algorithm below I cannot understand why the complexity is O(N+M),as calculated by codility.com examination tool, instead of be O(NM). 在下面的算法中,我无法理解为什么复杂性是O(N + M),由codility.com检查工具计算,而不是O(NM)。 I suppose it is O(NM) because of the two iteration one nested to the other one. 我认为它是O(NM),因为两个迭代一个嵌套到另一个迭代。

function solution($N, $A) {
// write your code in PHP7.0
$counter=[];
$max=0;

foreach($A as $value){
    if($value >= 1 && $value <= $N){
        isset($counter[$value-1]) ? $counter[$value-1]++ : $counter[$value-1] = 1;
        if($counter[$value-1] > $max){
            $max = $counter[$value-1];

            }
        }
    if($value == $N+1){
        for($i = 0; $i<$N; $i++){
            $counter[$i]=$max;
            }
        }
    }
return $counter;
}

Since I have not really understood the differences between the two complexities, can anybody give me two simple algorithms showing the two complexities? 由于我还没有真正理解这两种复杂性之间的差异,有人能给我两个简单的算法来展示这两种复杂性吗?

如果A数组中的所有值都等于N+1 ,则counter数组将被填充M=Length(A)次,因此最坏的情况是O(N*M) ,如果A内容没有特殊限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM