-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSubarray.java
More file actions
executable file
·155 lines (151 loc) · 5.68 KB
/
Copy pathMaximumSubarray.java
File metadata and controls
executable file
·155 lines (151 loc) · 5.68 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package facebook;
import java.util.Arrays;
/* * in-order, befroe/ after
* max sum
* local and global
* T:O(n!), try all possible combinations and add them up,
* so we have totally Cn1 + Cn2 + ... + Cnn = n! combinations.
* http://buttercola.blogspot.com/2014/08/leetcode-maximum-subarray.html
* */
public class MaximumSubarray {
/* maxSum
* Time: O(n), Space:O(1)
* local[i] = max(array[i] + local[i - 1], array[i])
* max = Math.max(local[i], max)
*/
public int maxSum(int[] array){
// Zero: null check
if (array == null || array.length == 0)
return 0;
// First: local = max
int local = array[0];
int global = array[0];
for (int i = 1; i < array.length; i++){
local = Math.max(local+array[i], array[i]);// accumulate, alone
global = Math.max(local, global);
}
return global;
}
/*maxSumRecursive
* Time:O(nlogn) merge sort, Space:O(logn)
* // Zero: null check
* // First: recursive with a global maximum
/*helper
* it could be actually four cases: left + mid, mid, right + mid, left + mid + right.
* However, if either left or right sum is less than zero,
* we can simply discard that part.
* That is why the mlMax and mrMax start from zero,
* and we simply calculate the left + mid + right which guarantee covering all cases above
* */
public int maxSumRecursive(int[] array){
// Zero: null check
if (array == null || array.length == 0)
return 0;
// First: recursive with a global maximum
int[] max = new int[2];
max[0] = Integer.MIN_VALUE;
return helper(array, 0 , array.length-1,max);
}
/*helper
* it could be actually four cases: left + mid, mid, right + mid, left + mid + right.
* However, if either left or right sum is less than zero,
* we can simply discard that part.
* That is why the mlMax and mrMax start from zero,
* and we simply calculate the left + mid + right which guarantee covering all cases above
* */
private int helper(int[] array, int l, int r, int[] max){
//Base case:
if (l > r){
return Integer.MIN_VALUE;
}
// Recursive case:
int mid = (l+r) /2;
int lMax = helper(array, l, mid-1, max);
int rMax = helper(array, mid+1, r, max);
max[0] = Math.max(max[0], lMax);
max[0] = Math.max(max[0], rMax);
// find max from middle to left
int mlMax = 0;
int sum = 0;
for (int i = mid -1; i >= l;i--){
sum += array[i];
mlMax = Math.max(mlMax, sum);
}
// find max from middle to right
int mrMax = 0;
sum = 0;
for (int j = mid+1; j<=r;j++){
sum += array[j];
mrMax = Math.max(mrMax, sum);
}
return Math.max(max[0], mrMax+mlMax+array[mid]);
}
public static void main(String[] args){
int[] array = new int[]{-2,1,-3,4,-1,2,1,-5,4};
MaximumSubarray sol = new MaximumSubarray();
System.out.println(Arrays.toString(array)+"\nmax subarray sum: "+sol.maxSum(array));
System.out.println(Arrays.toString(array)+"\nmax subarray sum(recursive): "+sol.maxSumRecursive(array));
}
}
/* 0 1 2 3 4 5 6 7 8
* -2,1,-3,4,-1,2,1,-5,4
*
* [4,−1,2,1] => 6
*
* h(A,0,8)
* m = 4
* int lMax = h(A, 0,3)
* m = 1
* int lMax = h(A,0,0)
* m = 0
* int lMax = h(A, 0,-1) = MIN
* int rMax = h(A,1,0) = MIN
* max= max(MIN, MIN) = MIN
* max= max(MIN, MIN) = MIN
* mlmax = 0
* mrmax = 0
* return max(MIN, mlmax+mrmax+A[mid])=-2
* int rMax = h(A,2,3)
* m =2
* int lMax = h(A,2,1) = MIN
* int rMax = h(A,3,3)
* m = 3
* int lMax = h(A,3,2) = MIN
* int rMax = h(A,4,3) = MIN
* max = max(MIN, MIN) = MIN
* max = MIN
* mlmax = 0
* mrmax = 0
* return max(MIN, mlamx+mrmax+A[mid]) = 4
* max = max(MIN, MIN) = MIN
* max = max(MIN,4) = 4
* mlmax = 0 =>0
* mrmax = 0 =>4
* return max()
* max = max(-2,MIN) = -2
* max = max(4, MIN) = 4
* mlmax = 0
* mrMax= 4
* return max(4, 0+0+1) = 4
* int rMax = h(A, 5,8)=>1,2
* mid = 6
* int lmax = h(A,5,5)
* ...
* return 2
* int rmax = h(A,7,8)
* mid= 7
* int lMax = MIN
* iny rMAX = h(A,8,8)
* ..
* return 4
* max = 4
* mlMax = 0
* mrMax = 4
* return 4
* max = 2,
* max =4,
* nlMax = 2
* nrMax = 4
* return
*
* */