Maximum Subarray
Given an integer array nums, find the contiguous subarray containing at least one number that has the largest sum, and return that sum.
Open official problem prompt ↗Locate the single contiguous stretch of the array whose elements add up to the greatest possible total.
Like walking a trail tracking your net elevation gain: if your accumulated climb ever drops below zero it is deadweight, so you reset your baseline at your feet and keep noting the highest point you have ever stood.
- Input
- nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
- Output
- 6
- Why
- The subarray [4, -1, 2, 1] has the largest sum, 6.
1 <= nums.length <= 10^5-10^4 <= nums[i] <= 10^4