-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1590_minSubarray.cc
62 lines (54 loc) · 1.36 KB
/
Problem_1590_minSubarray.cc
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
#include <iostream>
#include <unordered_map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// 满足(x - y) % p = 0,则称 x 与 y 模 p 同余,记 x ≡ y (mod p)
// 设所有元素和为x,去掉的元素和为y
// 要使得 (x - y) % p = 0,等价于 x 与 y 模 p 同余
int minSubarray(vector<int> &nums, int p)
{
int N = nums.size();
int ans = N;
vector<int> sum(N + 1);
for (int i = 0; i < N; i++)
{
sum[i + 1] = (sum[i] + nums[i]) % p;
}
int x = sum[N];
unordered_map<int, int> map;
for (int i = 0; i <= N; i++)
{
map[sum[i]] = i;
// 等价于在前缀数组上找 s[left] 和 s[right]
// 满足right - left最小,且 s[right] - s[left] ≡ x (mod p)
// 枚举right = i,尝试找left
auto it = map.find((sum[i] - x + p) % p);
if (it != map.end())
{
// 如果找到,就取最小区间
ans = std::min(ans, i - it->second);
}
}
return ans < N ? ans : -1;
}
};
void testMinSubarray()
{
Solution s;
vector<int> n1 = {3, 1, 4, 2};
vector<int> n2 = {6, 3, 5, 2};
vector<int> n3 = {1, 2, 3};
EXPECT_EQ_INT(1, s.minSubarray(n1, 6));
EXPECT_EQ_INT(2, s.minSubarray(n2, 9));
EXPECT_EQ_INT(0, s.minSubarray(n3, 3));
EXPECT_SUMMARY;
}
int main()
{
testMinSubarray();
return 0;
}