我在计算迭代依赖于外部循环的两组代码示例的Big O运行时遇到了一些麻烦。我对Big O运行时间有了基本的了解,并且我可以计算出更简单的代码示例的运行时间。我不太确定某些线路是如何影响运行时间的。嵌套循环的大O,有不同的迭代?
我会考虑这第一个O(n^2)。但是,我不确定。
for(i = 1; i < n; i++){
for(j = 1000/i; j > 0; j--){ <--Not sure if this is still O(n)
arr[j]++; /* THIS LINE */
}
}
我对这个有点失落。 O(n^3)可能是O(n^2)?
for(i = 0; i < n; i++){
for(j = i; j < n; j++){
while(j<n){
arr[i] += arr[j]; /* THIS LINE */
j++;
}
}
}
我发现这个职位和我申请这第一个代码示例,但我仍然不能确定第二。 What is the Big-O of a nested loop, where number of iterations in the inner loop is determined by the current iteration of the outer loop?
这应该是公认的答案OP对问题1的错误回答。 – meowgoesthedog
@meowgoesthedog同意 – UnknowableIneffable