Maximum Subarray
Medium Dynamic Programming
nums is a list of whole numbers, some of which may be negative or zero.
A stretch of nums is one or more numbers that sit next to each other, with none skipped between them. Its sum is those numbers added together.
Return the largest sum that any stretch has.
Example 1
Input: nums = [3,-8,5,2,-1,4]
Output: 10
The stretch 5, 2, -1 and 4 adds up to 10. Reaching back for the 3 would cost the -8 on the way, which loses more than the 3 gains.
Example 2
Input: nums = [-6,-2,-9]
Output: -2
Example 3
Input: nums = [4,-1,3,-10,5]
Output: 6
Constraints
1 ≤ nums.length ≤ 10⁵-10⁴ ≤ nums[i] ≤ 10⁴
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Dynamic Programming
- Best Time to Buy and Sell StockAI round · Easy
- Climbing StairsAI round · Easy
- Longest Increasing SubsequenceAI round · Medium
- Coin ChangeAI round · Medium
- Unique PathsAI round · Medium