-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContinuous Subarray Sum II.cpp
More file actions
32 lines (32 loc) · 1016 Bytes
/
Continuous Subarray Sum II.cpp
File metadata and controls
32 lines (32 loc) · 1016 Bytes
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
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) {
// Write your code here
int n = A.size();
if(n == 0) return {};
int l = 0, sum = A[0], maxs = A[0], al = 0, ar = 0;
int l2 = 0, sum2 = A[0], mins = A[0], ml = 0, mr = 0, totsum = A[0];
for(int i = 1; i < n; i++) {
totsum += A[i];
if(sum < 0) l = i;
sum = max(sum, 0)+A[i];
if(maxs < sum) {
al = l; ar = i;
maxs = sum;
}
if(sum2 > 0) l2 = i;
sum2 = min(sum2, 0)+A[i];
if(mins > sum2) {
ml = l2; mr = i;
mins = sum2;
}
}
if(mr-ml < n-1 && totsum-mins > maxs) return {mr+1, ml-1};
return {al, ar};
}
};