-
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
42 lines (39 loc) · 967 Bytes
/
Copy path209.minimum-size-subarray-sum.cpp
File metadata and controls
42 lines (39 loc) · 967 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
41
/*
* @lc app=leetcode id=209 lang=cpp
*
* [209] Minimum Size Subarray Sum
*/
// @lc code=start
#include "bits/stdc++.h"
using namespace std;
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
// sum of [l, r)
int prefix_sum = 0;
int l = 0, r = 0;
int res = nums.size() + 1;
while (l <= r)
{
if (prefix_sum < target) {
// boundary check
if (r < nums.size()) {
prefix_sum += nums[r++];
} else {
break;
}
} else if (prefix_sum >= target) {
res = min(res, r - l);
prefix_sum -= nums[l++];
}
}
return res == nums.size() + 1 ? 0 : res;
}
};
int main() {
Solution sol;
vector<int> nums = {1,1,1,1,1,1,1,1};
cout << sol.minSubArrayLen(11, nums) << endl;
return 0;
}
// @lc code=end