Maximum Product Subarray
Recognize the pattern
Brute force idea
A straightforward first read of Maximum Product Subarray is this: Check every subarray's product. Computes each product from scratch without reusing previous results. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.
Better approach
The deeper shift in Maximum Product Subarray is this: Track both current max and current min at each position. A negative number flips them — the most negative becomes the most positive when multiplied by a negative. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.
Key invariant
At the center of Maximum Product Subarray is one steady idea: At each element, the maximum product ending here is max(nums[i], maxSoFar * nums[i], minSoFar * nums[i]). You need both extremes because a negative can flip the minimum into the maximum. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.
Watch out for
A common way to get lost in Maximum Product Subarray is this: Only tracking the maximum — negative × negative = positive, so the minimum product can suddenly become the maximum. You MUST track both. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.