Largest Rectangle in Histogram
Signals to notice
Brute force first
For each bar, expand left and right to find the rectangle. Each bar re-scans for boundaries. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.
The key insight
Monotonic increasing stack: for each bar, pop all taller bars (they've found their right boundary). The rectangle height is the popped bar, width extends from the new stack top to the current index. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.
What must stay true
The stack holds bar indices in increasing height order. When a shorter bar arrives, all taller bars on the stack have found their right boundary — calculate their rectangles before pushing the shorter bar. As long as that statement keeps holding, you can trust the steps built on top of it.
Easy way to go wrong
Not processing remaining bars after the loop — bars still on the stack haven't found a right boundary, so their right boundary is the array end. The fix is usually to return to the meaning of each move, not just the steps themselves.