-
Notifications
You must be signed in to change notification settings - Fork 258
/
subarray-sum-ii.cpp
63 lines (57 loc) · 1.94 KB
/
subarray-sum-ii.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Time: O(nlogn)
// Space: O(n)
// Binary search solution.
class Solution {
public:
/**
* @param A an integer array
* @param start an integer
* @param end an integer
* @return the number of possible answer
*/
int subarraySumII(vector<int>& A, int start, int end) {
// sum_from_start[i] denotes sum for 0 ~ i - 1.
vector<int> sum_from_start(A.size() + 1);
partial_sum(A.cbegin(), A.cend(), sum_from_start.begin() + 1);
int result = 0;
for (int j = 0; j < A.size(); ++j) {
const auto left = lower_bound(sum_from_start.cbegin(),
sum_from_start.cbegin() + j + 1,
sum_from_start[j + 1] - end);
const auto right = upper_bound(sum_from_start.cbegin(),
sum_from_start.cbegin() + j + 1,
sum_from_start[j + 1] - start);
result += (right - sum_from_start.cbegin()) -
(left - sum_from_start.cbegin());
}
return result;
}
};
// Time: O(n^2)
// Space: O(n)
class Solution2 {
public:
/**
* @param A an integer array
* @param start an integer
* @param end an integer
* @return the number of possible answer
*/
int subarraySumII(vector<int>& A, int start, int end) {
// sum_from_start[i] denotes sum for 0 ~ i - 1.
vector<int> sum_from_start(A.size() + 1);
partial_sum(A.cbegin(), A.cend(), sum_from_start.begin() + 1);
int result = 0;
for (int i = 0, j = 0; j < A.size(); ++j) {
int k = i;
while (k <= j) {
if (sum_from_start[j + 1] - sum_from_start[k] >= start &&
sum_from_start[j + 1] - sum_from_start[k] <= end) {
++result;
}
++k;
}
}
return result;
}
};