Monotonic Stack
A monotonic stack keeps its elements in sorted order, popping any that violate it as you scan. It answers 'next greater' or 'next smaller element' style questions in O(n) instead of O(n²).
7 LeetCode problems solved with the Monotonic Stack pattern. Practice them with spaced repetition so the pattern sticks.
- Car FleetMEDIUM · O(n log n)
- Daily TemperaturesMEDIUM · O(n)
- Largest Rectangle in HistogramHARD · O(n)
- Maximum Binary TreeMEDIUM · O(n)
- Next Greater Element IEASY · O(n + m)
- Next Greater Element IIMEDIUM · O(n)
- Online Stock SpanMEDIUM · O(1)
See the full solution
The complete approach, reference solutions in 5 languages, and a step-by-step visualization — then add this problem to your spaced-repetition schedule.
Start free →