-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode209_minimum_size_subarray_sum.cpp
87 lines (73 loc) · 1.94 KB
/
leetcode209_minimum_size_subarray_sum.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @file leetcode209_minimum_size_subarray_sum.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/06/15 16:57
* @brief https://leetcode.com/problems/minimum-size-subarray-sum
*
**/
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int minLen = INT_MAX;
int b = 0;
int e = 0;
int len = nums.size();
int curSum = nums[0];
while (e < len) {
if (curSum < target) {
++e;
if (e >= len) {
break;
}
curSum += nums[e];
} else {
int curLen = e - b + 1;
if (curLen < minLen) {
minLen = curLen;
}
curSum -= nums[b];
++b;
}
}
while (b < e) {
curSum -= nums[b];
if (curSum >= target) {
int curLen = e - b;
if (curLen < minLen) {
minLen = curLen;
}
}
++b;
}
return (minLen == INT_MAX)? 0: minLen;
}
};
int main() {
int n;
std::vector<int> nums;
while (1) {
n = 0;
std::cout << "Number of elements (Ctrl-C to exit): " << std::endl;
if (!(std::cin >> n)) {
return 0;
}
std::cout << n << " integers: " << std::endl;
nums.clear();
int ele;
for (int i = 0; i < n; ++i) {
std::cin >> ele;
nums.push_back(ele);
}
std::cout << "Target: ";
int target;
cin >> target;
Solution solution;
auto ret = solution.minSubArrayLen(target, nums);
std::cout << ret << std::endl;
}
return 0;
}