Skip to content

Maximum Subarray

MediumGreedy
Decode it first. Clues, constraints, pattern and plan before any code.

Return the largest sum of any non-empty contiguous run of numbers in nums.

nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]  →  6    (4, -1, 2, 1)
nums = [5, 4, -1, 7, 8]                 →  23   (the whole list)
nums = [-3, -1, -2]                     →  -1   (you must pick at least one number)

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4