Temperature Tracker - Problem
Given an array of daily temperatures, return an array where each element represents the number of days you have to wait until a warmer temperature arrives. If there is no future day with a warmer temperature, use 0 instead.
Use a monotonic stack approach to solve this efficiently. The stack should maintain temperatures in decreasing order and help track indices of days waiting for warmer weather.
Example: For temperatures [73, 74, 75, 71, 69, 72, 76, 73], the output should be [1, 1, 4, 2, 1, 1, 0, 0] because day 0 (73°) finds a warmer day after 1 day (74°), day 2 (75°) finds a warmer day after 4 days (76°), and so on.
Input & Output
Example 1 — Basic Temperature Sequence
$
Input:
temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
›
Output:
[1, 1, 4, 2, 1, 1, 0, 0]
💡 Note:
Day 0 (73°): next warmer day is day 1 (74°) → wait 1 day. Day 1 (74°): next warmer day is day 2 (75°) → wait 1 day. Day 2 (75°): next warmer day is day 6 (76°) → wait 4 days. Day 6 and 7 have no warmer days ahead → 0.
Example 2 — Increasing Sequence
$
Input:
temperatures = [30, 40, 50, 60]
›
Output:
[1, 1, 1, 0]
💡 Note:
Each day finds a warmer day immediately next, except the last day which has no future days.
Example 3 — Decreasing Sequence
$
Input:
temperatures = [30, 60, 90]
›
Output:
[1, 1, 0]
💡 Note:
Day 0 finds warmer day 1, day 1 finds warmer day 2, day 2 has no warmer days ahead.
Constraints
- 1 ≤ temperatures.length ≤ 105
- 30 ≤ temperatures[i] ≤ 100
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code