[英]How to calculate the time complexity of a Java program?
How can I calculate the time complexity of this program?如何计算这个程序的时间复杂度? Is there a more efficient solution?有没有更有效的解决方案?
public class MinJumpstoEnd {
public static void main(String[] args) {
int[] a = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9};
//i is used for array elements & c to count jumps
int i =0,c =0;
int j = a[0];
while(a.length-i > j) {
if(a[i]==0) {
System.out.println("Cant Reach");
}
i += j;
c+=1;
j = a[i]-1;
System.out.println("j:"+j);
}
System.out.println("Jumps : "+c);
}
}
In the worst case your i has to traverse the whole array, and hence it would be O(n).在最坏的情况下,您的 i 必须遍历整个数组,因此它将是 O(n)。 It might happen that it might skip few elements in between while traversing but still it would be a linear function.它可能会在遍历时跳过几个元素,但它仍然是线性 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.