> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/02-05-2022-209.md).

# 02/05/2022 209

Method: use sliding window to solve this problem. Set left to the start of the window and right to the end of the window. Iterate each value, get the sum in the window, increment the right index. while the sum is greater or equal to the target, we deduct the nums\[left] until the value is still greater and equal to the target but the length is the smallest.&#x20;

Time O(n)

Space O(1)

class Solution { public int minSubArrayLen(int target, int\[] nums) { int left = 0;// the starting position of the window int sum = 0; int result = Integer.MAX\_VALUE;

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

}
