15% off all plans & roadmaps Use code
See plansLeetCode 152 · Solution & editorial
Given an integer array nums, find a subarray that has the largest product, and return the product.
Maximum Product 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 Product Subarray has been reported in interviews at Amazon, LinkedIn, Google, Apple, Facebook, Akuna Capital.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Maximum Product 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.