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
Daily Temperatures: Find Days Until Warmer WeatherInput Temperatures73747571Processing LogicFor each day, count days until next warmer temperatureDay 0 (73°): Day 1 (74°) is warmer → wait 1 dayDay 1 (74°): Day 2 (75°) is warmer → wait 1 dayOutput Days to Wait1142Result: [1, 1, 4, 2, ...]
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
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
255.0K Views
High Frequency
~25 min Avg. Time
8.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen