Skip to content
Optimal Lab

DSA · interactive

Monotonic stack: days until a warmer day

For each day, how long until it gets warmer? Checking forward from every day is O(n²). Keep a stack of days still waiting, coldest on top, and each day is pushed and popped at most once.

73°d0·
74°d1·
75°d2·
71°d3·
69°d4·
72°d5·
76°d6·
73°d7·
Stackbottom
  1. empty
top

Empty stack. Each day waits on the stack until a warmer day shows up.

0 stack operations · max 16
  1. 1

    Walk the days left to right, once.

    —

  2. 2

  3. 3

  4. 4

In production

Tools and technologies

“Next greater element” questions show up in stock spans, histograms and skyline problems.

Not tied to one language — use these names when you search docs or design a system.