-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSubarray.java
More file actions
30 lines (22 loc) · 836 Bytes
/
MaximumSubarray.java
File metadata and controls
30 lines (22 loc) · 836 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
package java_exercise;
public class MaximumSubarray {
/*
* Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
* For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
* the contiguous subarray [4,−1,2,1] has the largest sum = 6.
*/
// for an array A[0, i], if there is a k so that the sum from A[k] to A[i] is the maximum,
// we define it as max[i]. Then max[i+1] = max[i] + A[i+1] if (max[i] + A[i+1] > 0) or = 0
// if max[i+1] < 0. Because if max[i+1] < 0, A[i+1] must be a negative number. So we drop it.
//O(n) Solution
public int maxSubArray(int[] nums) {
int sum = 0, max = Integer.MIN_VALUE;
for (int i : nums) {
sum += i;
if (sum > max) max = sum;
if (sum < 0) sum = 0;
}
return max;
}
//Divide and Conquer Solution
}