class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int left = 0;
int sum = 0;
int ans = INT_MAX;
for(int right = 0; right < nums.size(); right++) {
sum += nums[right];
while(sum >= target) {
ans = min(ans, right - left + 1);
sum -= nums[left++];
}
}
return ans == INT_MAX ? 0 : ans;
}
};
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int i = 0;
int n = nums.length;
int sum = 0;
int ans = Integer.MAX_VALUE;
for(int j = 0; j < n; j++) {
sum += nums[j];
while(sum >= target) {
int subLength = j - i + 1;
ans = Math.min(ans, subLength);
sum -= nums[i++];
}
}
return ans != Integer.MAX_VALUE ? ans : 0;
}
}