Find the contiguous subarray with the largest sum and return its sum.
At each index, either extend the current subarray or start fresh, whichever is larger. A negative running sum only hurts, so drop it.
1function maxSubArray(nums: number[]): number {2let cur = nums[0], best = nums[0];3for (let i = 1; i < nums.length; i++) {4cur = Math.max(nums[i], cur + nums[i]);5best = Math.max(best, cur);6}7return best;8}
cur = best = nums[0] = -2.
Space: play/pause · ←/→: step