15% off all plans & roadmaps Use code
See plansLeetCode 53 · Solution & editorial
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Maximum Subarray is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Kadane's Algorithm for Max/Min Subarray) subpattern. The pattern covers fibonacci, Kadane, knapsack, LCS, LIS, edit distance, grid paths, and word break
Master Kadane's algorithm for finding maximum (or minimum) subarray sum in O(n) time.
The reference solution runs in O(n) time and O(1) space.
Maximum Subarray has been reported in interviews at Adobe, Amazon, Apple, Bloomberg, ByteDance, Cisco, Facebook, Google.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Maximum Subarray is a Dynamic Programming (DP) Patterns problem, in the 1D Array (Kadane's Algorithm for Max/Min Subarray) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.