-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0053.java
More file actions
78 lines (70 loc) · 2.18 KB
/
Copy pathLeetCode0053.java
File metadata and controls
78 lines (70 loc) · 2.18 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
/* Maximum Subarray
* Example:
* Input: [-2,1,-3,4,-1,2,1,-5,4],
* Output: 6
* Explanation: [4,-1,2,1] has the largest sum = 6.
* */
package Array;
import static java.lang.Math.*;
public class LeetCode0053 {
public static void main(String args[]){
int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
System.out.println(maxSubArray(nums));
}
/*public static int maxSubArray(int[] nums) {
int maxSoFar = 0;
int sum;
for(int i=0; i < nums.length; i++){
sum = 0;
for(int j = i; j < nums.length; j++){
sum += nums[j];
// sum is sum of x[i..j]
maxSoFar = max(maxSoFar, sum);
}
}
return maxSoFar;
}*/
/*public static int maxSubArray(int[] nums){
int max_so_far=nums[0];
int curr_max=nums[0];
for(int i=1;i<nums.length;i++){
curr_max = Math.max(nums[i],curr_max+nums[i]);
max_so_far=Math.max(max_so_far,curr_max);
}
return max_so_far;
}*/
public static int maxSubArray(int[] nums){
if (nums == null || nums.length == 0)
return 0;
return rec(nums, 0, nums.length - 1);
}
//返回这个之间的最大子序和
private static int rec(int[] arr, int L, int R) {
if (L == R)
return arr[L];
int mid = L + (R - L) / 2;
int LMax = rec(arr, L, mid);
int RMax = rec(arr, mid + 1, R);
int sum = 0, LSumMax = Integer.MIN_VALUE, RSumMax = Integer.MIN_VALUE;
for (int i = mid; i >= L; i--) {
sum += arr[i];
if (sum > LSumMax) {
LSumMax = sum;
}
}
sum = 0;
for (int i = mid + 1; i <= R; i++) {
sum += arr[i];
if (sum > RSumMax) {
RSumMax = sum;
}
}
int crossMax = LSumMax + RSumMax;
//compare crossMax、LMax,RMax
if (LMax >= RMax && LMax >= crossMax)
return LMax;
if (RMax >= LMax && RMax >= crossMax)
return RMax;
return crossMax;
}
}