forked from azheanda/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumsubarray2.java
More file actions
38 lines (28 loc) · 1.19 KB
/
maximumsubarray2.java
File metadata and controls
38 lines (28 loc) · 1.19 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
/*
Another solution to this problem to try divide and conquer as "more practice" indicates. The time complexity is O(nlogn).
*/
public class Solution {
public int maxSubArray(int[] A) {
return maxSubArray(A,0,A.length-1);
}
public int maxSubArray(int[] A,int l, int r){
int leftSum=Integer.MIN_VALUE,rightSum=Integer.MIN_VALUE,sum=0;
if(l==r) //base case
return A[l];
int mid = (l+r)/2;
int maxLeftSum = maxSubArray(A,l,mid);
int maxRightSum = maxSubArray(A,mid+1,r);
for(int i=mid;i>=l;i--){ // Note:this part is subtle.
sum+=A[i]; // The code in the brackets is equivalent to one line of code:
if(sum>leftSum) // leftSum = (sum+=A[i])>leftSum?sum:leftSum; // the parentheses are needed because the operator order of += is lower than ?:
leftSum =sum;
}
sum=0;
for(int i=mid+1;i<=r;i++){
sum+=A[i];
if(sum>rightSum)
rightSum=sum;
}
return Math.max(Math.max(maxLeftSum,maxRightSum),rightSum+leftSum);
}
}