Next Warmer Day in a Temperature Log
Stacks & QueuesAuto-graded · 17 test casesLast updated
Tests the ability to use a monotonic stack to efficiently find, for each day, how many days until a strictly warmer temperature is recorded.
Next Warmer Day in a Temperature Log
A weather station recorded daily high temperatures as a list of integers. For each day, you want to know how many days you have to wait until a day with a strictly higher temperature occurs.
If no such future day exists, the answer for that day is 0.
Given a list temps of n daily temperatures, return a list result of the same length where result[i] is the number of days after day i until a strictly warmer day, or 0 if no such day exists.
Constraints
1 ≤ len(temps) ≤ 2000-50 ≤ temps[i] ≤ 50- Temperatures are integers.
- The last element always gets answer
0(no future day exists). - Equal temperatures do not count as warmer.
Examples
Example 1
Input: next_warmer_day([30, 28, 32, 25, 35, 29])
Output: [2, 1, 2, 1, 0, 0]
Example 2
Input: next_warmer_day([20, 20, 20, 20])
Output: [0, 0, 0, 0]
Example 3
Input: next_warmer_day([15])
Output: [0]
Hints
Hint 1
Think about what information you need when you finally find a warmer day for a previous day — you need to know the index of that earlier day.
Hint 2
A stack that keeps indices of days whose 'next warmer day' hasn't been found yet is useful. What ordering property should those indices have with respect to their temperatures?
Hint 3
Maintain a stack of indices with non-increasing temperatures. When you encounter a day warmer than the top of the stack, you've found the answer for that top index: pop it, compute the difference, and repeat.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Intuition
For each day we need the nearest future day with a strictly higher temperature. A brute-force double loop works in O(n²), but a monotonic (non-increasing) stack lets us solve every day in amortized O(1).
We scan left to right, maintaining a stack of indices whose "next warmer day" is still unknown. The key invariant: the temperatures at those indices form a non-increasing sequence from bottom to top of the stack.
When we arrive at day i:
- While the stack is non-empty and
temps[i] > temps[stack[-1]], the current dayiis the first warmer day forstack[-1]. Pop it, and recordresult[prev] = i - prev. - Push
ionto the stack.
After the loop, any index still on the stack has no warmer future day, so its answer remains 0.
Why it is correct
Every index is pushed exactly once and popped at most once, so the total number of stack operations is O(n). The invariant ensures that, the moment we find a warmer temperature, we immediately resolve all stack entries it is warmer than — in order from most recent to oldest — which is exactly their "next warmer day".
Complexity
- Time: O(n) — each index is pushed and popped at most once.
- Space: O(n) — the stack can hold at most n indices (e.g., a strictly decreasing sequence).
Reference solution
def next_warmer_day(temps: list) -> list:
n = len(temps)
result = [0] * n
stack = [] # holds indices of days with unresolved "next warmer day"
for i in range(n):
# While the current day is warmer than the day at the top of the stack,
# the current day is the answer for that stacked day.
while stack and temps[i] > temps[stack[-1]]:
prev = stack.pop()
result[prev] = i - prev
stack.append(i)
# Any remaining indices in the stack have no warmer future day → result stays 0
return result