Stage 1 · Code
Stack & Binary Search
Monotonic Stack
Maintain a stack that is always sorted — the pattern behind next greater element, largest rectangle, and daily temperatures.
9 min readLeetCode Patterns for InterviewsCode
The Pattern
A monotonic stack keeps elements in sorted order (increasing or decreasing). When a new element violates the monotonicity, pop elements until the invariant holds — each popped element has found its 'next greater/smaller' answer.
Monotonic decreasing stack — each element is smaller than the one below it
Monotonic Stack
71top
72
75
Gonext-greater-element-pattern.go
30 linesLn 1, Col 1Go
The stack holds indices of elements waiting for their next greater element. When we see a larger element, every stack element smaller than it gets resolved.
Problems
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.