[英]What is time complexity for a nested for loop where the inner loop is for a different array?
[英]What will be the time complexity of this nested loop?
我有一个函数,它有嵌套循环,现在我无法计算循环的时间复杂度,因为内部循环没有运行 n 次,每次迭代的步骤也不是 +1 请帮助解释
int calculateJumps(int X, int Y, int n, int walls[])
{
int jumps = 0;
for(int i=0;i<n;i++) {
int reach = X;
int jump = 1;
while(reach < walls[i]) {
reach += (X-Y);
jump += 1;
}
jumps += jump;
}
return jumps;
}
外循环的迭代需要
Theta(walls[i]\/(XY))<\/code> (内循环的时间)。
因此,代码摘录的时间复杂度为
Theta(sum(walls)\/(XY))<\/code> 。
这里,
sum(walls)<\/code>表示数组
walls<\/code>的所有元素的总和。
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.