53. 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.
More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
Thoughts:
Divide and Conquer: For each subarray, calculate four values:
m (largest sum of this subarray),
l (largest sum starting from the left most element),
r (largest sum ending with the rightmost element),
s (the sum of the whole subarray).
Greedy: find the largest difference between the sums summing from left to right. The largest difference corresponds to the sub-array with largest sum.
DP: dp[i] : max sum till i.
dp[i] = max(dp[i-1], nums[i] + dp[i-1])
have a variable to record max dp[i] so far
Code (DP) Time Complexity O(n), Space Complexity O(n)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
if( n == 0 ) return 0;
if(n == 1) return nums[0];
int sum = nums[0];
int dp [n]; fill_n(dp, n, 0); dp[0] = nums[0];
for ( int i = 1; i < n; i++){
dp[i] = dp[i-1] > 0 ? dp[i-1] + nums[i]: nums[i];
if(dp[i] > sum) sum = dp[i];
}
return sum;
}
};Code (Divide and Conquer) Time Complexity O(nlogn), Space Complexity O(nlogn)
Code (Greedy) Time Complexity: O(n), Space Complexity: O(1)
Last updated
Was this helpful?