forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
43 lines (31 loc) · 948 Bytes
/
main.cpp
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
41
42
43
/// https://leetcode.com/problems/minimum-size-subarray-sum/description/
/// Author : liuyubobobo
/// Time : 2017-11-13
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
// Sum Prefix
// Time Complexity: O(n^2)
// Space Complexity: O(n)
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
assert(s > 0);
vector<int> sums(nums.size() + 1, 0);
for(int i = 1 ; i <= nums.size() ; i ++)
sums[i] = sums[i-1] + nums[i-1];
int res = nums.size() + 1;
for(int l = 0 ; l < nums.size() ; l ++)
for(int r = l ; r < nums.size() ; r ++)
if(sums[r+1] - sums[l] >= s)
res = min(res, r - l + 1);
return res == nums.size() + 1 ? 0 : res;
}
};
int main() {
vector<int> nums1 = {2, 3, 1, 2, 4, 3};
int s1 = 7;
cout << Solution().minSubArrayLen(s1, vec1) << endl;
return 0;
}