current position:Home>Why is the time complexity of nested while loops in for still o (n)

Why is the time complexity of nested while loops in for still o (n)

2022-02-02 21:13:52 CSDN Q & A

```
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int left = 0;
int result = Integer.MAX_VALUE;
int sum = 0;
for(int right = 0; right < nums.length; right++){
sum += nums[right];
while(sum >= target){
result = Math.min(result, (right - left + 1));
sum -= nums[left++];
}
}
return result == Integer.MAX_VALUE ? 0 : result;
}
}

```java

img


I don't quite understand the meaning of this sentence Can you help me analyze




Refer to the answer 1:

The outer update variable is right,right The scope is 0 ~ len - 1
The inner update variable is left,left The scope is also 0 ~ len - 1.
There is no correlation between the two , in other words right update ,left Not from scratch ,right Will only traverse once ,left Only once , So the complexity is 2 * n, And O(n)




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022113502141.html

Random recommended