-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
47 lines (42 loc) · 1.53 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
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
int maxSubArrayLen(vector<int>& nums, int k) {
// Initially thought of backtracking; but we want subarray, not a subseq.
// Sliding window may be plausible. The naive one, which would involve
// tracking a running sum is not plausible though, since its is not clear
// when to shrink the window.
// However, what about prefix sums? It allows us to query the sum of a
// range. Something like two-sum but with ranges instead of individual
// elements.
// Hmm. Iterate through the array, calculating the prefix sum
// nums[0..i] (inclusive).
// Look for its complement, i.e., sum(nums[0..i]) - k.
// {complement, index}
std::unordered_map<long long, int> prefixSums;
prefixSums[0] = -1; // handle the case where sum(nums[0..i]) == k
long long sum = 0;
int maxSize = 0;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
// We want sum(nums[0..i]) - sum(nums[0..j]) == k
// Therefore, sum(nums[0..j]) = sum(nums[0..i]) - k
auto iter = prefixSums.find(sum - static_cast<int>(k));
if (iter != prefixSums.end()) {
maxSize = std::max(maxSize, i - iter->second);
}
// hm. check if the prefix sum already exist. if it does, there is no
// point inserting this one.
if (!prefixSums.count(sum)) {
prefixSums[sum] = i;
}
}
return maxSize;
}
};