Daily Temperatures - Problem
Given an array of integers temperatures representing 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
Example 1 — Basic Case
$
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 is day 1 (74°) → wait 1 day. Day 1 (74°): Next warmer is day 2 (75°) → wait 1 day. Day 2 (75°): Next warmer is day 6 (76°) → wait 4 days. Day 6 and 7 have no warmer days → 0.
Example 2 — Single Temperature
$
Input:
temperatures = [30,40,50,60]
›
Output:
[1,1,1,0]
💡 Note:
Each day has the next day warmer except the last day: 30→40 (wait 1), 40→50 (wait 1), 50→60 (wait 1), 60→none (wait 0).
Example 3 — Decreasing Temperatures
$
Input:
temperatures = [30,60,90]
›
Output:
[1,1,0]
💡 Note:
Day 0: Next warmer at day 1 → wait 1. Day 1: Next warmer at day 2 → wait 1. Day 2: No warmer day → wait 0.
Constraints
- 1 ≤ temperatures.length ≤ 105
- 30 ≤ temperatures[i] ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of daily temperatures
2
Process
For each day, find next warmer day
3
Output
Days to wait or 0 if none
Key Takeaway
🎯 Key Insight: Use a monotonic decreasing stack to efficiently match each day with its next warmer day in linear time
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code