Given a list of daily temperatures, return how many days you must wait for a warmer temperature for each day.
Daily Temperatures
You are given an array of integers temperatures, where temperatures[i] is the temperature on day i.
For each day, find out how many days you would have to wait until a warmer temperature appears. If there is no future day with a warmer temperature, the answer for that day is 0.
Return an array answer such that answer[i] is the number of days after day i until a warmer temperature occurs.
The task is to process the array efficiently, especially when the number of days is large.
Input Format
- A single integer array
temperatures. temperatures[i]represents the temperature on dayi.
Output Format
- Return an integer array
answerof the same length. answer[i]is the number of days to wait for a strictly warmer temperature after dayi, or0if none exists.
Constraints
1 <= temperatures.length- Each
temperatures[i]is an integer. - The required result should be computed efficiently in linear time.
Example 1
Input
temperatures = [73,74,75,71,69,72,76,73]
Output
[1,1,4,2,1,1,0,0]
Explanation
- Day 0 waits 1 day for 74.
- Day 1 waits 1 day for 75.
- Day 2 waits 4 days for 76.
- Day 3 waits 2 days for 72.
- Day 4 waits 1 day for 72.
- Day 5 waits 1 day for 76.
- Days 6 and 7 have no warmer future day.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.