class Solution {
public:
int KHelper(vector<int>& nums, int s, int &sum, int &left, int right) {
while (sum >= s) {
sum -= nums[left];
left++;
}
sum += nums[left - 1];
left--;
return right - left + 1;
}
int minSubArrayLen(int s, vector<int>& nums) {
int left = 0, k = 0, right = left + k, sum = 0, N = nums.size(), st;
while (sum < s) {
if (right >= N) return 0;
sum += nums[right];
right++; //下一个元素
}
k = KHelper(nums, s, sum, left, right - 1);
if (k == 1) return 1;
st = nums[left];
while (right < N) {
sum += nums[right] - nums[left];
if ( sum >= s) {
//sum += nums[right] - nums[left];
left++;
k = KHelper(nums, s, sum, left, right);
if (k == 1) return 1;
right++;
}
else {
right++;
left++;
//sum += nums[right] - nums[left];
//st = nums[left];
}
}
return k;
}
};
int main()
{
Solution sol;
vector<int> nums = { 5,1,3,5,10,7,4,9,2,8};
int s = 15;
cout << sol.minSubArrayLen(s, nums);
cout << endl;
return 0;
}