Description
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
|
|
Explanation: [4,-1,2,1] has the largest sum = 6. Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
Dynamic Programming Solution
Let $dp[i]$ to be the maximum subarray value that ends at $nums[i]$, which is all the possible subarray sum which ends at $i$, i.e.
$$ \begin{aligned} dp[i] &= \max_{j \in [0, i] } \left\lbrace \sum\limits_{k=j}^{i} nums[k] \right\rbrace \\ &= \max_{j \in [0, i] } \left\lbrace \sum\limits_{k=j}^{i-1} nums[k],0 \right\rbrace + nums[i] \\ &= \max \left\lbrace dp[i-1], 0 \right\rbrace + nums[i] \end{aligned} $$
|
|
Notes
- When calculating
dp[i-1]
, watch out for the corner case. - Generate
max_sum
in each iter on the fly.