# LeetCode-Minimum Size Subarray Sum-解题报告

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.

For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.

class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
if (nums.empty())return 0;
long long sum = 0, cnt = 1, start = 0, ans = nums.size() + 1;
for (int i = 0; i < nums.size(); ++i)
{
sum += nums[i];
cnt++;
if (sum >= s)
{
while (sum - nums[start] >= s)
sum -= nums[start], start++, cnt--;
if (ans > cnt)ans = cnt;
}

}
return ans > nums.size()? 0 : ans - 1;
}
};

#### *LeetCode-Minimum Size Subarray Sum

2015-09-29 05:27:43

#### leetcode-Minimum Size Subarray Sum

2015-05-12 10:01:07

#### Minimum Size Subarray Sum-最小长度字数组和问题

2016-05-19 19:44:30

#### [LeetCode325] Maximum Size Subarray Sum Equals k

2016-01-08 15:34:02

#### [leetcode] 325. Maximum Size Subarray Sum Equals k 解题报告

2016-03-06 05:50:30

#### leetcode 209. Minimum Size Subarray Sum-子序列最小长度|双指针|二分查找

2016-06-13 22:14:10

#### leetcode 209 : Minimum Size Subarray Sum

2015-05-14 06:15:11

#### LeetCode 209 - Minimum Size Subarray Sum

2015-05-12 12:50:20

#### leetcode--- Minimum Size Subarray Sum

2016-11-22 22:22:49