-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
48 lines (42 loc) · 1.48 KB
/
Solution.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
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
int minSubarray(vector<int>& nums, int p) {
// The sum of the subarrays to be removed is a multiple of the remainder,
// sum = k * r, where k >= 0
// In other words, find subarrays that are divisible by R. i.e. reducible
// to a similar problem we have seen before!
// 974.Subarray Sums Divisble by K requires counting the number of subarray
// with sums divisible by K. sum{nums[i:j]} % k = 0
// Here, we only want the smallest subarray.
// Similarly, we can use the property of modulo;
// ((prefixSum{nums[:j]} % k) - (prefixSum{nums[:i]} % k)) % k = 0
// prefixSum{nums[:j]} % k = prefix{nums[:i]} % k
int remainder = 0;
for (const int num : nums) {
remainder = (remainder + num) % p;
}
if (remainder == 0) {
return 0;
}
// Using a vector will result in OOM, since we are storing prefix mod p
unordered_map<int, int> mods;
mods[0] = -1;
int currSum = 0;
int minLen = nums.size();
for (int i = 0; i < nums.size(); ++i) {
// Calculate the prefix sum mod p
currSum = (currSum + nums[i]) % p;
// From this prefix sum, get the value that needs to be removed
int target = (currSum - remainder + p) % p;
if (mods.count(target)) {
minLen = min(minLen, i - mods[target]);
}
mods[currSum] = i;
}
return minLen == nums.size() ? -1 : minLen;
}
};