-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxSubSum.java
119 lines (103 loc) · 3.12 KB
/
MaxSubSum.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package org.algo.base;
/***
* 53. Maximum Subarray
*/
public class MaxSubSum {
/***
* O(N³)
* Cubic maximum contiguous subsequence sum algorithm.
*/
public int maxSubSumCubic(int[] a) {
int maxSum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
int thisSum = 0;
for (int k = i; k <= j; k++) {
thisSum += a[k];
}
if (thisSum > maxSum) maxSum = thisSum;
}
}
return maxSum;
}
/***
* O(N²)
* Quadratic maximum contiguous subsequence sum algorithm.
*/
public int maxSubSumQuad(int[] a) {
int maxSum = 0;
for (int i = 0; i < a.length; i++) {
int thisSum = 0;
for (int j = i; j < a.length; j++) {
thisSum += a[j];
if (thisSum > maxSum) maxSum = thisSum;
}
}
return maxSum;
}
// O(N*lgN)
public int maxSubSumLog(int[] a) {
return maxSubSumRec(a, 0, a.length - 1);
}
/***
* O(N) Linear-time
*
* Dynamic Programming:
* dp[i] : max sum subarray ending with index i
* dp[i] = max(dp[i-1] + a[i], a[i])
*/
public int maxSubArray(int[] a) {
int maxValue = a[0];
int n = a.length;
int[] dp = new int[n + 1];
dp[0] = a[0];
for (int i = 1; i < n; i++) {
dp[i] = Math.max(dp[i - 1] + a[i], a[i]);
maxValue = Math.max(dp[i], maxValue);
}
return maxValue;
}
/***
* O(N) Linear-time maximum contiguous subsequence sum algorithm.
*/
public int maxSubSumLinear(int[] a) {
int max = 0, maxSoFar = 0;
for (int i = 0; i < a.length; i++) {
maxSoFar += a[i];
if (maxSoFar > max) max = maxSoFar;
else if (maxSoFar < 0) maxSoFar = 0;
}
return max;
}
/***
* Recursive maximum contiguous subsequence sum algorithm.
* Finds maximum sum in subarray spanning a[left..right].
* Does not attempt to maintain actual best sequence.
*/
private int maxSubSumRec(int[] a, int l, int r) {
if (l >= r) return Math.max(0, a[l]);
int m = l + (r - l) / 2;
int maxLeft = maxSubSumRec(a, l, m);
int maxRight = maxSubSumRec(a, m + 1, r);
int leftSideMax = 0, leftSumMax = 0;
for (int i = m; i >= l; i--) {
leftSumMax += a[i];
if (leftSumMax > leftSideMax) {
leftSideMax = leftSumMax;
}
}
int rightSideMax = 0, rightSumMax = 0;
for (int i = m + 1; i <= r; i++) {
rightSumMax += a[i];
if (rightSumMax > rightSideMax) {
rightSideMax = rightSumMax;
}
}
return Math.max(Math.max(maxLeft, maxRight), leftSideMax + rightSideMax);
}
public static void main(String[] args) {
MaxSubSum maxSubSum = new MaxSubSum();
System.out.println(
"Result : " + maxSubSum.maxSubArray(new int[] {4, -3, 5, -2, -1, 2, 6, -2}));
}
}