-
Notifications
You must be signed in to change notification settings - Fork 257
/
continuous-subarray-sum-ii.cpp
55 lines (51 loc) · 1.43 KB
/
continuous-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
// Time: O(n)
// Space: O(1)
class Solution {
public:
/**
* @param A an integer array
* @return A list of integers includes the index of
* the first number and the index of the last number
*/
vector<int> continuousSubarraySumII(vector<int>& A) {
vector<int> result{-1, -1};
int max_sum = numeric_limits<int>::min();
int total = 0;
// Non-circular subarray.
int sum = 0;
int start = 0, end = 0;
for (int i = 0; i < A.size(); ++i) {
total += A[i];
if (sum < 0) {
sum = A[i];
start = end = i;
} else {
sum += A[i];
end = i;
}
if (sum >= max_sum) {
max_sum = sum;
result[0] = start;
result[1] = end;
}
}
// Circular subarray.
sum = 0;
start = 0, end = -1;
for (int i = 0; i < A.size(); ++i) {
if (sum > 0) {
sum = A[i];
start = end = i;
} else {
sum += A[i];
end = i;
}
if (total - sum > max_sum && !(start == 0 && end == A.size() - 1)) {
max_sum = total - sum;
result[0] = (end + 1) % A.size();
result[1] = (start - 1) % A.size();
}
}
return result;
}
};