Stack
Medium

Daily Temperatures

Find how many days you have to wait for a warmer temperature.
Problem 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.

  1. Initialize answer array with 0s.
  2. Use a stack to store indices of days.
  3. Iterate through temperatures.
  4. 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].
  5. Push current index i onto the stack.
Interactive Visualization
Step 1 / 1
Empty Stack
Top Index: -1

Initializing...

1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.
Unlock VisualizerPREMIUM FEATURE

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.

Start Your Premium Prep