Daily Temperatures
MediumStack · NVIDIA
You are given an array temperatures, where temperatures[i] is the temperature on day i.
Return an array answer of the same length where answer[i] is the number of days you have to wait after day i until a strictly warmer day arrives. A day with the same temperature does not count as warmer.
If no later day is warmer than day i, set answer[i] to 0.
Example 1
- Input:
- temperatures = [72, 68, 70, 69, 75, 71]
- Output:
- [4, 1, 2, 1, 0, 0]
- Explanation
- Day 0 (72) waits until day 4 (75), which is 4 days. Day 1 (68) warms up the next day. Day 2 (70) skips the cooler 69 and waits 2 days for 75. Days 4 and 5 never see anything warmer, so they get 0.
Example 2
- Input:
- temperatures = [70, 70, 71]
- Output:
- [2, 1, 0]
- Explanation
- Equal is not warmer, so day 0 does not stop at day 1 and waits 2 days for the 71.
Example 3
- Input:
- temperatures = [80, 79, 78]
- Output:
- [0, 0, 0]
- Explanation
- Temperatures only fall, so no day ever has a warmer day after it.
Constraints
- 1 ≤
temperatures.length≤ 105 - 30 ≤
temperatures[i]≤ 100