← Monotonic Stack

Pattern Recognition Drill

#135 — Daily Temperatures

Medium Stacks & Queues

The Problem

Given daily temperatures, for each day find how many days until a warmer temperature.

What approach would you use?

Think about it before scrolling down.

Key Signals

Monotonic Stack

Decreasing stack of indices. Pop while current temp > stack top. Distance = current - popped index. O(n).

Alt: Stack

Same approach. Each element pushed and popped at most once.

Common Trap

Don't scan forward from each day — that's O(n²). The monotonic stack makes it one pass.

← #134