mediumStack / QueuePure DSA~25 min
Next Warmer Day
A weather widget annotates each day with how many days you must wait until a warmer day arrives. Given daily temperatures, produce for each day the number of days until a strictly higher temperature, or 0 if none follows.
Problem
Given an array temps, return an array answer where answer[i] is the number of days after day i until a warmer temperature. If no warmer day exists, answer[i] = 0.
Input
An array temps of length n (1 ≤ n ≤ 10^5), each value in [−50, 60].
Output
An array of n integers — the wait counts.
Constraints
- 1 ≤ n ≤ 100,000
- −50 ≤ temps[i] ≤ 60
- 'Warmer' means strictly greater
Examples
Example 1
Input
temps = [30, 31, 28, 33, 32]
Output
[1, 2, 1, 0, 0]
Day 0 waits 1 day for 31; day 1 waits 2 days for 33; day 2 waits 1 day for 33; days 3 and 4 have no warmer future day.
Example 2
Input
temps = [40, 40, 40]
Output
[0, 0, 0]
No strictly warmer day ever follows (equal does not count).