-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
43 lines (38 loc) · 1.11 KB
/
main.cpp
File metadata and controls
43 lines (38 loc) · 1.11 KB
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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int minSubArrayLen(int target, vector<int>& nums)
{
int start = 0, end = 0, current_sum = nums[0], minimal_size = 0;
while (start < (int)nums.size() && end < (int)nums.size())
{
// cout << "current window - start: " << start << "; end: " << end << "; sum: " << current_sum << endl;
if (current_sum >= target)
{
if (minimal_size == 0)
minimal_size = end - start + 1;
else
minimal_size = min(minimal_size, end - start + 1);
// shrink the window
current_sum -= nums[start];
start++;
}
else // expand the window
{
end++;
if (end < (int)nums.size())
current_sum += nums[end];
}
}
return minimal_size;
}
};
int main()
{
vector<int> nums = {2,3,1,2,4,3};
int target = 7;
cout << Solution().minSubArrayLen(target, nums) << endl;
return 0;
}