-
Notifications
You must be signed in to change notification settings - Fork 15
/
maximum-subarray-ii.java
46 lines (37 loc) · 1.33 KB
/
maximum-subarray-ii.java
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
public class Solution {
/**
* @param nums: A list of integers
* @return: An integer denotes the sum of max two non-overlapping subarrays
*/
public int maxTwoSubArrays(ArrayList<Integer> nums) {
// write your code
// maxInorder, maxReverse
int size = nums.size();
int[] maxInorder = new int[size];
int[] maxReverse = new int[size];
int preMax = nums.get(0);
int max = preMax;
maxInorder[0] = preMax;
for (int i = 1; i < size; i++) {
int current = nums.get(i);
preMax = Math.max(preMax + current, current);
max = max < preMax? preMax : max;
maxInorder[i] = max;
}
preMax = nums.get(size - 1);
maxReverse[size - 2] = preMax;
max = preMax;
for (int i = size - 3; i >= 0; i--) {
int current = nums.get(i + 1);
preMax = Math.max(preMax + current, current);
max = max < preMax? preMax : max;
maxReverse[i] = max;
}
int maxSum = maxInorder[0] + maxReverse[0];
for (int i = 0; i < size - 1; i++) {
int tempSum = maxInorder[i] + maxReverse[i];
maxSum = maxSum < tempSum? tempSum : maxSum;
}
return maxSum;
}
}