-
Notifications
You must be signed in to change notification settings - Fork 22
/
402.continuous-subarray-sum.cpp
54 lines (51 loc) · 1.37 KB
/
402.continuous-subarray-sum.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
// Tag: Prefix Sum Array, Array
// Time: O(N)
// Space: O(N)
// Ref: -
// Note: -
// Given an integer array, find a continuous subarray where the sum of numbers is the biggest.
// Your code should return the index of the first number and the index of the last number.
// (If their are duplicate answer, return the minimum one in lexicographical order)
//
// **Example 1:**
//
// ```
// Input: [-3, 1, 3, -3, 4]
// Output: [1, 4]
// ```
//
// **Example 2:**
//
// ```
// Input: [0, 1, 0, 1]
// Output: [0, 3]
// Explanation: The minimum one in lexicographical order.
// ```
//
//
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> continuousSubarraySum(vector<int> &a) {
// write your code here
int n = a.size();
vector<int> prefix(n + 1, 0);
int l = 0;
int r = 0;
int min_index = 0;
for (int i = 0; i < n; i++) {
prefix[i + 1] = prefix[i] + a[i];
if (prefix[i + 1] - prefix[min_index] > prefix[r + 1] - prefix[l]) {
l = min_index;
r = i;
}
if (prefix[min_index] > prefix[i + 1]) {
min_index = i + 1;
}
}
return vector<int> {l, r};
}
};