Maximum Subarray
Medium Dynamic Programming AI round
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⁴
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.
Your first AI round is free.
Watch 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
- House RobberMedium