Daily Temperatures
Given an array temperatures of daily temperatures, return an array answer of the same length where answer[i] is the number of days you have to wait after day i for a strictly warmer temperature. If no warmer day ever comes, answer[i] is 0.
The array can be up to 10^5 entries, so the O(n^2) "for each day, scan forward" approach is too slow. This is the classic next greater element problem in disguise.
Examples
- Input:
- temperatures = [73,74,75,71,69,72,76,73]
- Output:
- [1,1,4,2,1,1,0,0]
Day 0 (73) waits 1 day for 74. Day 2 (75) waits 4 days: 71, 69, 72 are all cooler; 76 on day 6 is the first warmer. The last day has nothing after it, so 0.
- Input:
- temperatures = [30,40,50,60]
- Output:
- [1,1,1,0]
Rising temperatures: each day is answered by the next day.
- Input:
- temperatures = [60,50,40,30]
- Output:
- [0,0,0,0]
Falling temperatures: no day ever sees a warmer one.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.