-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.java
48 lines (45 loc) · 1.41 KB
/
Solution.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
package _053;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/05/04
* desc :
* </pre>
*/
public class Solution {
// public int maxSubArray(int[] nums) {
// int len = nums.length, dp = nums[0], max = dp;
// for (int i = 1; i < len; ++i) {
// dp = nums[i] + (dp > 0 ? dp : 0);
// if (dp > max) max = dp;
// }
// return max;
// }
public int maxSubArray(int[] nums) {
return helper(nums, 0, nums.length - 1);
}
private int helper(int[] nums, int left, int right) {
if (left >= right) return nums[left];
int mid = (left + right) >> 1;
int leftAns = helper(nums, left, mid);
int rightAns = helper(nums, mid + 1, right);
int leftMax = nums[mid], rightMax = nums[mid + 1];
int temp = 0;
for (int i = mid; i >= left; --i) {
temp += nums[i];
if (temp > leftMax) leftMax = temp;
}
temp = 0;
for (int i = mid + 1; i <= right; ++i) {
temp += nums[i];
if (temp > rightMax) rightMax = temp;
}
return Math.max(Math.max(leftAns, rightAns), leftMax + rightMax);
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums0 = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
System.out.println(solution.maxSubArray(nums0));
}
}