Maximum Subarray

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write maxSubarray(nums): return the largest sum of a run of one or more neighbouring numbers in nums. nums has at least one number and may hold negatives; if every number is negative, the answer is the largest one on its own. Leave nums unchanged. For example, maxSubarray([-2, 1, -3, 4, -1, 2, 1, -5, 4]) is 6, from 4, -1, 2, 1. The checks include 100,000 numbers, so adding up every run is too slow: walk through the list once, keeping the best sum of a run that ends at each number.

Your code must pass

  • Finds the largest sum of a run
  • Returns the largest number when every number is negative
  • Handles a single number
  • Is fast enough for 100,000 numbers