-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path209_Minimum Size Subarray Sum.cpp
More file actions
40 lines (32 loc) · 975 Bytes
/
209_Minimum Size Subarray Sum.cpp
File metadata and controls
40 lines (32 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Given an array of n positive integers and a positive integer s, find
// the minimal length of a contiguous 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.
// 解法:sliding window
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int min_len = nums.size() + 1, sum = 0;
for (int left = 0, right = 0; right < nums.size(); ++right) {
// expand
sum += nums[right];
while (sum >= s) {
min_len = min(min_len, right - left + 1);
// shrink
sum -= nums[left++];
}
}
// final answer
return (min_len == nums.size() + 1) ? 0 : min_len;
}
};
int main() {
vector<int> nums = {1,2,3,4,5};
Solution s;
cout << s.minSubArrayLen(16,nums) << endl;
return 0;
}