Daily Temperatures
Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.
Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Explanation: For day 0 (73°), the next warmer day is day 1 (74°), so answer[0] = 1.
For day 2 (75°), the next warmer day is day 6 (76°), so answer[2] = 4.
The last two days have no warmer future day, so they remain 0.
Example 2:
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Explanation: Each day's next warmer temperature is the very next day, except the last day.
Example 3:
Input: temperatures = [30,60,90]
Output: [1,1,0]
Examples
Example 1
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Explanation: For day 0 (73°), the next warmer day is day 1 (74°). For day 2 (75°), the next warmer day is day 6 (76°). The last two days have no warmer future day.
Example 2
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Explanation: Each day's next warmer temperature is the very next day, except the last day which has no warmer future day.
Example 3
Input: temperatures = [30,60,90]
Output: [1,1,0]
Explanation: Day 0 waits 1 day for 60°, day 1 waits 1 day for 90°, and day 2 has no warmer day ahead.
Constraints
- -1 <= temperatures.length <= 10^5
- -30 <= temperatures[i] <= 100
Optimal Complexity
Time
O(n)
Space
O(n)
Practice this problem with an AI interviewer
TechInView conducts a full voice mock interview — the AI asks clarifying questions, evaluates your approach, watches you code, and scores you on 5 dimensions. Just like a real FAANG interview.
Start a free interview