Daily Temperatures Visualization & Animation
Returns the number of days until a warmer temperature for each day using a monotonic decreasing stack; O(n).
## What is it?
Given an array of daily temperatures, find how many days until a warmer temperature for each day. If no warmer day exists in the future, output 0.
## How it works
- Use a monotonic decreasing stack storing indices
- For each day `i`:
- While `temperatures[i] > temperatures[stack.top()]` → the wait for the top index is `i - stack.top()` days; pop
- Push `i`
- Indices remaining on the stack have no warmer future day → their answer is 0
## When to use
- "How many steps until condition X is satisfied" for each element
- Any problem involving nearest future element satisfying a monotone condition
- Classic interview problem demonstrating monotonic stack
## Key Points
- O(n) time — each index pushed and popped once
- O(n) space for the stack
- The answer array is filled backwards as greater elements are found
- Great example of converting O(n²) brute force to O(n) with a stack
Category: algorithms
Difficulty: intermediate
- monotonic-stack
- stack
Time Complexity: O(n)
Space Complexity: O(n)
View Daily Temperatures VisualizationDaily Temperatures
intermediateReturns the number of days until a warmer temperature for each day using a monotonic decreasing stack; O(n).
Init
empty
Monotonic Stack
i
73°
[0]74°
[1]75°
[2]71°
[3]69°
[4]72°
[5]76°
[6]73°
[7]Wait (days)
?
[0]?
[1]?
[2]?
[3]?
[4]?
[5]?
[6]?
[7]Initialize: empty stack, all answers = ? (unresolved).
1 / 37