-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMaximumSubarray.java
More file actions
30 lines (29 loc) · 839 Bytes
/
MaximumSubarray.java
File metadata and controls
30 lines (29 loc) · 839 Bytes
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
public class MaximumSubarray {
public int maxSubArray(int[] nums) {
int start = 0, end = -1;
int res = Integer.MIN_VALUE;
int sum = 0;
while (end < nums.length) {
if (sum < 0) {
sum = 0;
start = end + 1;
} else {
end++;
if (end > nums.length - 1) break;
sum += nums[end];
res = Math.max(res, sum);
}
}
return res;
}
// dp
public int maxSubArray1(int[] nums) {
int max_local = nums[0];
int max_global = nums[0];
for (int i = 1; i < nums.length; i++) {
max_local = Math.max(max_local + nums[i], nums[i]);
max_global = Math.max(max_global, max_local);
}
return max_global;
}
}