-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path209.cpp
More file actions
33 lines (25 loc) · 660 Bytes
/
209.cpp
File metadata and controls
33 lines (25 loc) · 660 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
#include<iostream>
#include<vector>
#define INT_MAX 2003403242
int minSubArrayLen(int target, std::vector<int>& nums) {
int min = INT_MAX;
int sum = 0;
int left = 0;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
while (sum >= target) {
min = std::min(min, i - left + 1);
sum -= nums[left];
++left;
}
}
return min == INT_MAX ? 0 : min;
}
int main() {
std::vector<int> nums = { 2,3,1,2,4,3 };
// std::vector<int> nums = { 1, 2, 3, 4, 5 };
int target = 7;
// int target = 11;
std::cout << minSubArrayLen(target, nums);
std::cin.get();
}