-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSumSubarray_53.java
More file actions
50 lines (45 loc) · 1.54 KB
/
MaximumSumSubarray_53.java
File metadata and controls
50 lines (45 loc) · 1.54 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
/**
* ----------------------------------------------------------------------------
Maximum Subarray
- 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.
* ----------------------------------------------------------------------------
*/
/**
* Related: 152 Maximum Product Subarray
* Tags: Dynamic Programming, Two Pointers
*/
/**
* Keywords: Contiguous
* - Normal way to do this is O(n^2), two loops
* - F[i] : the maximum subarray sum ending with i (must inlucde A[i])
* - sum: the maximal sum till A[i] (not necessary include A[i])
* - Time Compleixty: O(n) and Space O(1)
*/
public class Solution {
public int maxSubArray(int[] A) {
int sum = A[0], isum = A[0]; // F[i]
for(int i=1; i<A.length; i++) {
isum = Math.max(A[i], isum + A[i]);
sum = Math.max(sum, isum);
}
return sum;
}
}
/**
* Some other thoughts:
* - 1. The hint asks for divide & conquer
* - divide the array half each time
* - the maximum sum comes from three ways:
* 1). the left;
* 2). the right;
* 3). sum across the mid O(n):
* prefix in the left, and suffix in the right
* T(n) = T(n/2)*2 + O(n) => O(nlogn)
*
* - 2. Return Window Rather than the Sum
* - The ``contiguous'' suggests a two pointer solution
* - shift the current left pointer to i if (isum == A[i])
*/