-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_subarray_len.cpp
More file actions
53 lines (49 loc) · 1.26 KB
/
min_subarray_len.cpp
File metadata and controls
53 lines (49 loc) · 1.26 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
44
45
46
47
48
49
50
51
52
53
/*
* =====================================================================================
*
* Filename: min_subarray_len.cpp
*
* Description: 209. Minimum Size Subarray Sum
*
* Version: 1.0
* Created: 10/06/2025 00:00:42
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <algorithm>
#include <vector>
#include "gtest/gtest.h"
// Sliding window
// Time complexity: O(n)
class Solution {
public:
int minSubArrayLen(int target, std::vector<int>& nums) {
int left = 0;
int right = 0;
int len = INT_MAX;
int sum = 0;
for (; right < nums.size(); right++) {
sum += nums[right];
while (sum >= target) {
len = std::min(len, right - left + 1);
sum -= nums[left++];
}
}
return (len == INT_MAX ? 0 : len);
}
};
TEST(Solution, minSubArrayLen) {
std::vector<std::tuple<int, std::vector<int>, int>> cases = {
{7, {2, 3, 1, 2, 4, 3}, 2},
{4, {1, 4, 4}, 1},
{11, {1, 1, 1, 1, 1, 1, 1, 1}, 0},
};
for (auto& [target, nums, len] : cases) {
EXPECT_EQ(Solution().minSubArrayLen(target, nums), len);
}
}