Stack
Medium
Daily Temperatures
Find how many days you have to wait for a warmer temperature.
10 min read
Solve on LeetCodeProblem Understanding
Given an array of integers temperatures represents 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.
Algorithm Strategy
We use a Monotonic Decreasing Stack.
- Initialize
answerarray with 0s. - Use a stack to store indices of days.
- Iterate through
temperatures. - While the current temperature is warmer than the temperature at the index stored in
stack.top():- We found a warmer day for
stack.top()! - Pop the index (
prevIndex). - Calculate days waiting:
i - prevIndex. - Update
answer[prevIndex].
- We found a warmer day for
- Push current index
ionto the stack.
Interactive Visualization
Step 1 / 1
Empty Stack
Top Index: -1Initializing...
1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.ON THIS PAGE
- Problem Understanding
- Algorithm Strategy
- Interactive Visualization
- Dry Run
- Edge Cases & Common Mistakes
- Solution
Stop Guessing, Start Mastering.
Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.
