leetcode/solution/0200-0299/0209.Minimum Size Subarray Sum/Solution2.java

16 lines
398 B
Java

class Solution {
public int minSubArrayLen(int target, int[] nums) {
int l = 0, n = nums.length;
long s = 0;
int ans = n + 1;
for (int r = 0; r < n; ++r) {
s += nums[r];
while (s >= target) {
ans = Math.min(ans, r - l + 1);
s -= nums[l++];
}
}
return ans > n ? 0 : ans;
}
}