#169MediumStacks· Monotonic Stack
Days Until Warmer
Input format
`temps`: an array of integers, the noon temperature for each day.
Output format
An array of the same length: for each day the number of days to wait for a strictly higher temperature, or 0.
Examples
Input: temps = [73, 74, 75, 71, 69, 72, 76, 73]
Output: [1, 1, 4, 2, 1, 1, 0, 0]
Day 0 (73) waits one day for 74. Day 2 (75) waits four days for 76. Day 6 (76) and day 7 (73) never see a warmer day, so they get 0.
Input: temps = [30, 40, 50, 60]
Output: [1, 1, 1, 0]
Each day is followed immediately by a warmer one, except the last.
Input: temps = [30, 20, 10]
Output: [0, 0, 0]
Temperatures only fall, so nobody waits for a warmer day and everything is 0.
Constraints
- 1 <= temps.length <= 10^5
- -50 <= temps[i] <= 60
- #monotonic-stack
- #stack
- #arrays