Example 1
- Input
- temperatures = [73,74,75,71,69,72,76,73]
- Output
- [1,1,4,2,1,1,0,0]
Day 0 waits 1 day for 74, day 2 waits 4 days for 76, and the last two days never see a warmer day.
Given each day temperature, return for every day how many days ahead the next strictly warmer temperature occurs, or 0 when no later day is warmer.
Given an array of integers temperatures representing 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.
Only a strictly warmer day counts: equal temperatures do not resolve a wait. For temperatures = [73,74,75,71,69,72,76,73] the answer is [1,1,4,2,1,1,0,0].
1 <= temperatures.length <= 10^530 <= temperatures[i] <= 100Day 0 waits 1 day for 74, day 2 waits 4 days for 76, and the last two days never see a warmer day.
Each day is beaten by the very next day except the last, which waits for nothing.
Days 0 and 1 each wait exactly one day; day 2 has no warmer future day.
The only day has no later day to inspect, so its wait stays zero.
The equal day 1 does not resolve day 0. Day 2 at 71 resolves days 0 and 1 at distances 2 and 1. No later day is warmer than 71, and the final day has no future.
A day waits for the first later temperature strictly above its own. In Example 1 day 2 at 75 ignores 71,69,72 until day 6 at 76, so it waits 4 days. Keep unresolved day indices together; their temperatures never increase from bottom to top. A new warmer day resolves the colder indices on top immediately, while equal days stay waiting. Indices, rather than temperatures alone, let us compute the waiting distance.
Reveal Brute Force: intuition, complexity, and code
[100,90,80]: nothing ever pops and every answer stays 0.[30,40,50,60]: each day pops immediately and every answer except the last is 1.[70,70,70]: strict comparison never triggers, so every answer stays 0.i - prev cannot be computed.>= instead of strictly >: equal days are not warmer, and popping them assigns answers that should stay 0.For next-greater style waits, keep unresolved indices on a monotonic stack so each arrival settles exactly the days it relieves.