Daily Temperatures

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Daily Temperatures

Approach

Maintain a stack of unresolved indices with decreasing temperatures. When a warmer day arrives, repeatedly pop colder indices and fill their wait times. Push current index afterward.

Complexity Analysis

Time
O(n)
Space
O(n)

Pattern

Monotonic Stack (Decreasing)

Why It Works

Each index enters and leaves the stack at most once. The stack invariant guarantees the first warmer day for a popped index is the current index.

Updated Feb 2026