Problem
FAANG · LeetCode #739

Daily Temperatures — three-stage drill

1The ProblemMedium

Daily Temperatures

For each day, how many days until a warmer temperature.

days until a warmer temperaturetemps730741752713764answer11210return[1, 1, 2, 1, 0]
Examples
[73,74,75,71,76]→ [1,1,2,1,0]

constraint0 if no warmer day follows.

2The PatternTIME O(n) · SPACE O(n)

Monotonic Stack

A stack kept sorted; each new value pops everything it resolves, so each element is pushed and popped once — O(n).

a bigger value pops every smaller one, then is pushed3011421354scan›stack — kept decreasingbottom3top is always the smallest — so it pops firststack empty → push 3
Problems using this pattern
3The SolutionRESULT → [1,1,2,1,0]

Step through it

temps730741752713764ans01234stack of indices — temps decreasing(empty)top = smallest → it pops firsti = today's index (pointer) · j = the index being resolved
stack holds indices of days still waiting — kept in decreasing temperature
Step 1 / 11
Arrow keys to navigate · escape to reset

All problems by pattern

Backtracking

Recursive Descent