← Stack Discipline

Micro-Drill #38 — Monotonic stack template

Stack Discipline Target: 10s

Monotonic stacks find next-greater/smaller elements in O(n). Key for histogram and temperature problems.

stack = []
for x in a:
    while stack and stack[-1] >= x:
        stack.pop()
    stack.append(x)

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #37 Micro #39 →