Daily Temperatures
Find how many days you must wait for a warmer temperature using a monotonic decreasing stack of day indices in linear time.
01The problem
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 i-th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.
| Input | Output | Why |
|---|---|---|
[73, 74, 75, 71, 69, 72, 76, 73] | [1, 1, 4, 2, 1, 1, 0, 0] | Day 0 (73) waits 1 day for 74; Day 2 (75) waits 4 days for 76 |
[30, 40, 50, 60] | [1, 1, 1, 0] | Every day except the last is followed by a warmer day immediately |
[30, 60, 90] | [1, 1, 0] | Day 0 waits 1 day; Day 1 waits 1 day; Day 2 has no warmer future day |
Constraints: 1 <= temperatures.length <= 10^5, 30 <= temperatures[i] <= 100.
Loading the rest of the lesson…