Browse Curriculum
FAANGPrep Sprint
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

Visualize how the monotonic stack keeps track of 'unresolved' cooler days.

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.