-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMaximumSubArray.java
30 lines (28 loc) · 967 Bytes
/
MaximumSubArray.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
import java.util.Arrays;
public class MaximumSubArray {
public static void main(String[] args) {
// keep adding to get current Sum, if nums[i] > current Sum, then skip to i.
int[] nums = {1,-1,2,0};
System.out.println(maxSubArray(nums));
}
public static int maxSubArray(int[] nums) {
int max = nums[0];
int current_sum = max;
for (int i = 1; i < nums.length; i++){
current_sum = Math.max(nums[i] + current_sum, nums[i]);
max = Math.max(max, current_sum);
}
return max;
// for (int i = 0; i < nums.length; i++) {
// for (int j = i; j < nums.length; j++) {
// int total = 0;
// for (int j2 = i; j2 <= j; j2++) {
// total += nums[j2];
// }
// if (total > sum) {
// sum = total;
// }
// }
// }
}
}