-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSubarray.java
More file actions
57 lines (54 loc) · 1.77 KB
/
Copy pathMaximumSubarray.java
File metadata and controls
57 lines (54 loc) · 1.77 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
/**
* Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
*
* For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
* the contiguous subarray [4,−1,2,1] has the largest sum = 6.
*
* click to show more practice.
*
* More practice:
* If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which
* is more subtle.
*
* Solution reference: http://fisherlei.blogspot.com/2012/12/leetcode-maximum-subarray.html
*
* @author joshluo
*
*/
public class MaximumSubarray {
public int maxSubArray(int[] A) {
assert (A != null && A.length > 0);
int start = 0, end = A.length - 1;
return maxSubArray(A, start, end);
}
private int maxSubArray(int[] A, int start, int end) {
if (start > end) {
return Integer.MIN_VALUE;
}
int mid = (start + end) / 2;
int leftMax = maxSubArray(A, start, mid - 1);
int rightMax = maxSubArray(A, mid + 1, end);
// get the max around A[mid]
int midMax = A[mid], sum = A[mid];
for (int i = mid - 1; i >= start; i--) {
sum += A[i];
if (sum > midMax) {
midMax = sum;
}
}
sum = midMax;
for (int i = mid + 1; i <= end; i++) {
sum += A[i];
if (sum > midMax) {
midMax = sum;
}
}
int maxOfLeftAndRight = Math.max(leftMax, rightMax);
return Math.max(maxOfLeftAndRight, midMax);
}
public static void main(String[] args) {
MaximumSubarray solution = new MaximumSubarray();
int[] A = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
System.out.println(solution.maxSubArray(A));
}
}