For each day, how many days until a warmer one?
Question
You get a list of daily temperatures. For each day, return how many days you must wait for a strictly warmer day. If no warmer day comes, use 0. Aim for O(n) time.
days_until_warmer([18, 16, 16, 20, 17, 20, 22, 15]) # [3, 2, 1, 3, 1, 1, 0, 0] days_until_warmer([25, 24, 23]) # [0, 0, 0] days_until_warmer([]) # []
def days_until_warmer(temps): ...
Show hints (3)
- "WHO IS WAITING"
Read the days left to right. Some earlier days are still waiting for a warmer day. When a warm day arrives, which of the waiting days does it answer?
- "WHICH STRUCTURE"
Keep the waiting days on a stack. Store their indexes, not their temperatures, so you can compute the wait as
day - indexwhen you answer one. - "THE LOOP"
For each new day, pop every waiting day that is colder than today and record its wait. Then push today. Days still on the stack at the end keep 0.
Show checked explanation
Walk the days left to right and keep a stack of indexes of days that still wait. While today is warmer than the day on top, pop it and record today - that_day. Then push today. Each index is pushed and popped at most once, so the whole pass is O(n).
def days_until_warmer(temps): wait = [0] * len(temps) waiting = [] # indexes of days with no warmer day yet for day, temp in enumerate(temps): while waiting and temps[waiting[-1]] < temp: cold_day = waiting.pop() wait[cold_day] = day - cold_day waiting.append(day) return wait print(days_until_warmer([18, 16, 16, 20, 17, 20, 22, 15])) print(days_until_warmer([25, 24, 23])) print(days_until_warmer([]))
[3, 2, 1, 3, 1, 1, 0, 0] [0, 0, 0] []
Code run on CPython 3.12 against the examples above. Checked by an editor other than the author.
Walkthrough
Here is [18, 16, 16, 20, 17, 20, 22, 15]. The last column shows the temperatures of the waiting days, from the bottom of the stack to the top.
| day | temp | popped, with its wait | stack after |
|---|---|---|---|
| 0 | 18 | none | 18 |
| 1 | 16 | none | 18, 16 |
| 2 | 16 | none | 18, 16, 16 |
| 3 | 20 | day 2 (1), day 1 (2), day 0 (3) | 20 |
| 4 | 17 | none | 20, 17 |
| 5 | 20 | day 4 (1) | 20, 20 |
| 6 | 22 | day 5 (1), day 3 (3) | 22 |
| 7 | 15 | none | 22, 15 |
Days 6 and 7 are still on the stack at the end, so they keep 0.
Look at day 5. It is 20, the same as day 3. Equal is not warmer, so day 3 stays on the stack and waits for day 6. That is why the loop pops only while the top is strictly colder.
From bottom to top, the temperatures on the stack never go up. A colder day under a warmer one would already have been popped. This is why it is called a monotonic stack. It also lets the loop stop early: if today is not warmer than the top, it is not warmer than anyone below it.
Day 3 pops three days in one step, so a single step can cost O(n). But each index is pushed once and popped at most once. Over the whole list that is at most 2n stack operations, so the total time is O(n). The stack and the answer list take O(n) extra memory.
Common mistakes
- Pushing temperatures instead of indexes. When a warm day pops a 16, you don't know which 16 it was, so you can't compute how long it waited.
- Popping on
<=instead of<. Then an equal day counts as warmer. Day 1 gets 1 instead of 2, and day 3 gets 2 instead of 3. - Scanning forward from every day with a nested loop. On falling temperatures that is n(n-1)/2 comparisons: about 5 billion for 100,000 days.
- Calling the stack version O(n²) because it has a
whileinside afor. Count the pops instead: each day is popped at most once in total.
Follow-up questions
- "Now a day counts if it is at least as warm. What changes?" Pop on
<=instead of<, so an equal day answers the waiting one. For[12, 12, 10, 14, 13]the strict version gives[3, 2, 1, 0, 0], and the new one gives[1, 2, 1, 0, 0]. The time stays O(n). - "The temperatures arrive one per day, as a stream. When can you answer for a day?" On the day it is popped, which is the first day that is warmer. The stack holds exactly the days with no answer yet. If the temperature keeps falling, nothing is popped, and the stack grows to n.
- "How would you find the previous warmer day instead?" Use the same stack, but read the answer before you push. Pop every day that is not warmer than today. The day left on top is the closest earlier warmer day. An empty stack means there is none.
What a strong answer covers
Asks whether an equal day counts as warmer. Stores indexes, not values. Explains why the pass is O(n) even though one day can pop many. Tests duplicates, a falling list and an empty list.
Open in interactive practice deck →