For each day, report how long the wait is until a strictly warmer day occurs.
Problem
Input. A list temperatures of integer daily temperatures in chronological order.
Output. Return an equally long list whose entry at day i is the distance to its first warmer later day.
Rules. Warmer means strictly greater; an equal temperature does not qualify.
Return zero when no later day qualifies, not the number of remaining days.
Preserve the input. An empty list returns [].
Example. [73, 74, 75, 71, 69, 72, 76, 73] returns [1, 1, 4, 2, 1, 1, 0, 0].
Day 2 waits four days for 76; day 6 has no warmer later day.
Picture: Resolve Only the Colder Waiting Days
Problem trace
Separate longer array: resolve colder waiting indices at their first warmer day
- top waiting day
- (0, 0)
- temperatures
- [73, 74, 75, 71, 69, 72, 76, 73]
- layout
- oldest to newest
- current
- before day 0→day 0: 73
- action
- initialize→push 0
- column 0 / row 0
- empty→day 0: 73
- current
- day 0: 73→day 1: 74
- answer
- [0,0,0,0,0,0,0,0]→[1,0,0,0,0,0,0,0]
- action
- push 0→pop 0; 1 - 0 = 1; push 1
- current
- day 1: 74→day 2: 75
- answer
- [1,0,0,0,0,0,0,0]→[1,1,0,0,0,0,0,0]
- action
- pop 0; 1 - 0 = 1; push 1→pop 1; 2 - 1 = 1; push 2
- top waiting day
- (0, 0)→(1, 0)
- current
- day 2: 75→day 3: 71
- action
- pop 1; 2 - 1 = 1; push 2→71 <= 75; push 3
- top waiting day
- (1, 0)→(2, 0)
- current
- day 3: 71→day 4: 69
- action
- 71 <= 75; push 3→69 <= 71; push 4
- top waiting day
- (2, 0)→(1, 0)
- current
- day 4: 69→day 5: 72
- answer
- [1,1,0,0,0,0,0,0]→[1,1,0,0,1,0,0,0]
- answer
- [1,1,0,0,1,0,0,0]→[1,1,0,2,1,0,0,0]
- action
- pop 4; 5 - 4 = 1→pop 3; 5 - 3 = 2; push 5
- column 0 / row 1
- day 3: 71→day 5: 72
- top waiting day
- (1, 0)→(0, 0)
- current
- day 5: 72→day 6: 76
- answer
- [1,1,0,2,1,0,0,0]→[1,1,0,2,1,1,0,0]
- answer
- [1,1,0,2,1,1,0,0]→[1,1,4,2,1,1,0,0]
- action
- pop 5; 6 - 5 = 1→pop 2; 6 - 2 = 4; push 6
- column 0 / row 0
- day 2: 75→day 6: 76
- top waiting day
- (0, 0)→(1, 0)
- current
- day 6: 76→day 7: 73
- action
- pop 2; 6 - 2 = 4; push 6→73 <= 76; push 7
- Recognize it
- Look for the first later greater value or the waiting distance to it; those words suggest keeping unresolved indices until a new value is large enough to answer them.
- Keep true
- After each day is appended, waiting indices increase from bottom to top while their temperatures are non-increasing, and every popped index receives its first strictly warmer later day.
- Reuse it
- For next-greater or next-smaller problems, store unresolved indices in a monotonic stack; the first value that violates the monotonic order resolves every eligible item popped from the top.
Reference drawing
Problem trace
A warmer arrival resolves only colder waiting indices; answers measure index distance
Execution trace
- top waiting day
- (0, 0)
- temperatures
- [73, 74, 75, 71, 69, 72, 76, 73]
- layout
- oldest to newest
- current
- before day 0→day 0: 73
- action
- initialize→push 0
- column 0 / row 0
- empty→day 0: 73
- current
- day 0: 73→day 1: 74
- answer
- [0,0,0,0,0,0,0,0]→[1,0,0,0,0,0,0,0]
- action
- push 0→pop 0; 1 - 0 = 1; push 1
- current
- day 1: 74→day 2: 75
- answer
- [1,0,0,0,0,0,0,0]→[1,1,0,0,0,0,0,0]
- action
- pop 0; 1 - 0 = 1; push 1→pop 1; 2 - 1 = 1; push 2
- top waiting day
- (0, 0)→(1, 0)
- current
- day 2: 75→day 3: 71
- action
- pop 1; 2 - 1 = 1; push 2→71 <= 75; push 3
- top waiting day
- (1, 0)→(2, 0)
- current
- day 3: 71→day 4: 69
- action
- 71 <= 75; push 3→69 <= 71; push 4
- top waiting day
- (2, 0)→(1, 0)
- current
- day 4: 69→day 5: 72
- answer
- [1,1,0,0,0,0,0,0]→[1,1,0,0,1,0,0,0]
- answer
- [1,1,0,0,1,0,0,0]→[1,1,0,2,1,0,0,0]
- action
- pop 4; 5 - 4 = 1→pop 3; 5 - 3 = 2; push 5
- column 0 / row 1
- day 3: 71→day 5: 72
- top waiting day
- (1, 0)→(0, 0)
- current
- day 5: 72→day 6: 76
- answer
- [1,1,0,2,1,0,0,0]→[1,1,0,2,1,1,0,0]
- answer
- [1,1,0,2,1,1,0,0]→[1,1,4,2,1,1,0,0]
- action
- pop 5; 6 - 5 = 1→pop 2; 6 - 2 = 4; push 6
- column 0 / row 0
- day 2: 75→day 6: 76
- top waiting day
- (0, 0)→(1, 0)
- current
- day 6: 76→day 7: 73
- action
- pop 2; 6 - 2 = 4; push 6→73 <= 76; push 7
- Recognize it
- Look for the first later greater value or the waiting distance to it; those words suggest keeping unresolved indices until a new value is large enough to answer them.
- Keep true
- After each day is appended, waiting indices increase from bottom to top while their temperatures are non-increasing, and every popped index receives its first strictly warmer later day.
- Reuse it
- For next-greater or next-smaller problems, store unresolved indices in a monotonic stack; the first value that violates the monotonic order resolves every eligible item popped from the top.
Indices increase from bottom to top; their temperatures are nonincreasing, so equal values can remain together. This order makes every resolvable colder day accessible through consecutive pops at the top.
| New day | Resolution | Still waiting afterward |
|---|---|---|
| 3: 71 | None | Days 2, 3 |
| 4: 69 | None | Days 2, 3, 4 |
| 5: 72 | Day 4 waits 1; day 3 waits 2 | Days 2, 5 |
| 6: 76 | Day 5 waits 1; day 2 waits 4 | Day 6 |
| 7: 73 | None | Days 6, 7; final sentinels stay zero |
If an earlier warmer day existed, it would already have popped the waiting index.
Thus a pop certifies the first qualifying day, not merely some warmer day.
Store indices, not just temperatures: day-earlier_day supplies the distance, and equal values can have different waits.
Code
def daily_temperatures(temperatures: list[int]) -> list[int]:
answer = [0] * len(temperatures)
waiting: list[int] = []
for day, temperature in enumerate(temperatures):
while waiting and temperatures[waiting[-1]] < temperature:
earlier_day = waiting.pop()
answer[earlier_day] = day - earlier_day
waiting.append(day)
return answer
Time and space are under ordinary comparison costs. Each index is pushed once and popped at most once; some never pop. Total inner-loop work is linear.
Change 1: Equal Temperature Also Qualifies
New task. Find the first later day at least as warm.
Picture and change. Pop with <=; equal days now resolve, leaving strictly decreasing waiting temperatures.
Code.
def days_until_at_least(temperatures):
answer = [0] * len(temperatures)
waiting = []
for day, temperature in enumerate(temperatures):
while waiting and temperatures[waiting[-1]] <= temperature:
earlier = waiting.pop()
answer[earlier] = day - earlier
waiting.append(day)
return answer
Check. [70, 70, 71] gives [1, 1, 0], instead of the original [2, 1, 0].
Use when. Equality qualifies; this is a changed contract, not an optimization.
Change 2: Find the Next Strictly Smaller Value
New task. Return distances to the first later smaller value.
Picture and change. Reverse the inequality; waiting values become nondecreasing. Keep indices and the no-answer zero sentinel.
Code.
def days_until_smaller(values):
answer = [0] * len(values)
waiting = []
for index, value in enumerate(values):
while waiting and values[waiting[-1]] > value:
earlier = waiting.pop()
answer[earlier] = index - earlier
waiting.append(index)
return answer
Check. [3, 2, 2, 1] gives [1, 2, 1, 0]; equal 2 does not resolve the earlier 2.
Use when. The qualifying direction reverses while the first-answer proof still applies.
Failure: Losing Strictness or Position
Replacing < with <= incorrectly accepts equal temperatures under the original task.
Storing only values loses the coordinates needed for wait distances.
Transfer Check
Why is index order alone insufficient?
Reasoned Answer
It cannot identify which waiting days the new value resolves. Temperature monotonicity makes eligible indices contiguous at the top.
Unresolved Does Not Mean Impossible
During an ongoing stream, a waiting day may still receive a warmer future day. The final zero sentinel means no such day occurred within the completed input, not that none could ever occur.
Equal values must remain separately represented for strictly warmer queries.
In [70, 70, 71], the two 70-degree days resolve together but have waits 2 and 1.
Deduplicating them would lose an output position.
Both adaptations keep the one-push/at-most-one-pop accounting and linear auxiliary/output storage. Changing a threshold or adding expiry rules needs another proof of which waiting entries a new item can safely resolve. The bracket stack also stores unfinished work, but its pop rule follows nesting rather than temperature order.