LeetCode 152 ·
Medium
Maximum Product Subarray
Find the contiguous subarray with the largest product.
Try it
Step through the core mechanic. The simulator below runs the dynamic programming shape this problem is built on.
Walk the pattern
No dedicated step-through for this one yet. The shape is Dynamic programming — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.
The approach
Carry both the running max and running min ending here, because a negative number swaps them. Each step recomputes both from {x, max·x, min·x}; track the global max.
| Aspect | Value |
|---|---|
| Pattern | Dynamic programming |
| Recognise it by | Max product — track min too, for sign flips. |
| Time complexity | O(n) |
| Space complexity | O(1) |
| Difficulty | Medium |
Who asks it
Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.