LC 152
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.

AspectValue
PatternDynamic programming
Recognise it byMax product — track min too, for sign flips.
Time complexityO(n)
Space complexityO(1)
DifficultyMedium

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.