#7 most askedVery commonMediumGreedy
Maximum Subarray
New to Greedy? Learn it from scratch firstGreedy Algorithms โ
๐ข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.Find the contiguous subarray with the largest sum and return that sum.
[-2, 1, -3, 4, -1, 2, 1, -5, 4] โ 6 (the subarray [4, -1, 2, 1]).
๐งช Practice
Write your solution, then press Run tests. Explain your approach out loud as you go.
def max_subarray(nums):
pass
Output appears here. Press Run or Ctrl/โ + Enter.
Try it first. Struggling for a few minutes is how it sticks.