-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_0209_minSubArrayLen.cc
More file actions
54 lines (49 loc) · 996 Bytes
/
Problem_0209_minSubArrayLen.cc
File metadata and controls
54 lines (49 loc) · 996 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
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int minSubArrayLen(int target, vector<int> &nums)
{
int n = nums.size();
vector<int> pre(n + 1);
for (int i = 0; i < n; i++)
{
pre[i + 1] = pre[i] + nums[i];
}
int l = 0;
int r = 1;
int ans = INT32_MAX;
while (l < r && r < n)
{
while (r < n && pre[r] - pre[l] < target)
{
r++;
}
while (l < r && pre[r] - pre[l] >= target)
{
ans = std::min(ans, r - l);
l++;
}
}
return ans == INT32_MAX ? 0 : ans;
}
};
void testMinSubArrayLen()
{
Solution s;
vector<int> n1 = {2, 3, 1, 2, 4, 3};
vector<int> n2 = {1, 4, 4};
vector<int> n3 = {1, 1, 1, 1, 1, 1, 1, 1};
EXPECT_EQ_INT(2, s.minSubArrayLen(7, n1));
EXPECT_EQ_INT(1, s.minSubArrayLen(4, n2));
EXPECT_EQ_INT(0, s.minSubArrayLen(11, n3));
EXPECT_SUMMARY;
}
int main()
{
testMinSubArrayLen();
return 0;
}