209. Minimum Size Subarray Sum
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.Input: target = 4, nums = [1,4,4]
Output: 1Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0public int minSubArrayLen(int target, int[] nums) {
int min = nums.length + 1, l = 0, sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
while (sum >= target) {
min = Math.min(min, i - l + 1);
// remove the first element from the window
sum -= nums[l];
// move the start of window --> left
l++;
}
}
return min > nums.length ? 0 : min;
}Last updated