Math Fundamentals18 sections · 814 units
Open in Course

Practice - Maximum Subarray

LeetCode 53

Given an integer array numsnums, find the contiguous subarray with the largest sum and return its sum.

For example, if nums=[2,1,3,4,1,2,1,5,4]nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4], the answer is 66 (subarray [4,1,2,1][4, -1, 2, 1]).

Hint: at each position, decide whether to extend the current subarray or start a new one. This uses algebraic comparison: is current+nums[i]current + nums[i] better than just nums[i]nums[i]?