75. Maximum Subarray

Find the contiguous subarray (at least one element) with the largest sum and return that sum.

Example 1

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: [4, −1, 2, 1] has the largest sum, 6.

Example 2

Input: nums = [-3,-1,-2]
Output: -1
Explanation: All values are negative: pick the largest single value.
Hint 1

If the running sum becomes worse than starting fresh at the current element, restart.

Dynamic ProgrammingKadane