How it works
How it works
Before pushing a new element, pop elements that break the desired order, resolving answers as you pop.
Common problems
Next greater element, daily temperatures, and largest rectangle in a histogram.
Why it is fast
Each element is pushed and popped at most once, giving linear time overall.