Daily Temperatures - Problem

Imagine you're a weather forecaster tasked with helping people plan their activities. Given an array of daily temperatures, you need to determine how many days each person must wait to experience a warmer day.

For each day i, calculate the number of days until a temperature higher than temperatures[i] occurs. If no warmer day exists in the future, return 0 for that day.

Example: If temperatures are [73, 74, 75, 71, 69, 72, 76, 73], then:

  • 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 5 (72°): Next warmer day is day 6 (76°) → wait 1 day

This is a classic "next greater element" problem that can be solved efficiently using a monotonic stack.

Input & Output

example_1.py — Basic Temperature Sequence
$ Input: [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
💡 Note: Day 0 (73°): Day 1 has 74° → wait 1 day. Day 1 (74°): Day 2 has 75° → wait 1 day. Day 2 (75°): Day 6 has 76° → wait 4 days. Day 3 (71°): Day 5 has 72° → wait 2 days. Day 4 (69°): Day 5 has 72° → wait 1 day. Day 5 (72°): Day 6 has 76° → wait 1 day. Days 6,7: No warmer days ahead → 0.
example_2.py — Increasing Temperatures
$ Input: [30,40,50,60]
Output: [1,1,1,0]
💡 Note: Each day finds a warmer temperature the very next day, except the last day which has no future days.
example_3.py — Decreasing Temperatures
$ Input: [30,60,90]
Output: [1,1,0]
💡 Note: Day 0 finds warmer day at index 1, day 1 finds warmer day at index 2, day 2 has no warmer future day.

Visualization

Tap to expand
Weather Station Temperature TrackingTemperatureMonitorPending Days StackDay 3 (71°)Day 4 (69°)← Stack TopResult BoardDay 0: 1 day wait ✓Day 1: 1 day wait ✓Day 2: 4 days wait ✓Day 3: ? days waitDay 4: ? days waitDay 5: pending...New ReadingResolve & Record🌡️ Each warmer day can resolve multiple pending days instantlyStack ensures O(n) time - each day processed exactly once
Understanding the Visualization
1
Station Setup
Weather station initializes with empty stack and result board
2
Daily Reading
Each day, check if current temperature resolves any pending days in stack
3
Stack Maintenance
Pop resolved days, record their wait times, then add current day to pending stack
4
Completion
Days remaining in stack have no future warmer days (already set to 0)
Key Takeaway
🎯 Key Insight: The monotonic stack eliminates redundant comparisons by maintaining only the temperatures that could potentially be resolved by future warmer days, achieving optimal linear time complexity.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Each element is pushed and popped from stack at most once, resulting in linear time

n
2n
Linear Growth
Space Complexity
O(n)

In worst case (decreasing temperatures), stack can contain all n elements

n
2n
Linearithmic Space

Constraints

  • 1 ≤ temperatures.length ≤ 105
  • 30 ≤ temperatures[i] ≤ 100
  • All temperatures are valid Fahrenheit temperatures
Asked in
Amazon 42 Google 38 Meta 31 Microsoft 28 Apple 22
89.7K Views
High Frequency
~15 min Avg. Time
2.8K 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