---
title: 'Daily Temperatures: Monotonic Stack Solution'
source: 'https://youtube.com/watch?v=q0wMdnKYt4M'
video_id: 'q0wMdnKYt4M'
date: 2026-08-04
duration_sec: 83
---

# Daily Temperatures: Monotonic Stack Solution

> Source: [Daily Temperatures: Monotonic Stack Solution](https://youtube.com/watch?v=q0wMdnKYt4M)

## Summary

This video explains how to solve the 'Daily Temperatures' coding interview problem efficiently. It demonstrates a monotonic stack approach that reduces the time complexity from O(N^2) to O(N), making it suitable for large inputs like a million days.

### Key Points

- **Problem Definition** [00:01] — Given daily temperatures, for each day, count how many days until the next warmer day.
- **Naive Solution and Its Flaw** [00:14] — The naive approach scans forward for each day, resulting in O(N^2) time complexity, which is impractical for large inputs (e.g., a million days would take a trillion operations).
- **Monotonic Stack Approach** [00:26] — Use a stack of indices for days still waiting for a warmer day. When a warmer day appears, pop all colder days from the stack and record the gap for each.
- **Trace Example** [00:39] — First three days get cooler, so their indices are pushed onto the stack (0,1,2). When 72 appears, it's warmer than 69 (index 2), so pop and record 1. Then it's warmer than 71 (index 1), pop and record 2.
- **Complexity Analysis** [01:05] — Each index is pushed and popped once, so the algorithm runs in O(N) time, collapsing the O(N^2) scan into a single linear sweep.
- **Key Takeaway** [01:18] — When you need to find the next greater element to the right, use a monotonic stack.

### Conclusion

The monotonic stack technique is a powerful optimization for problems involving next greater elements, reducing time complexity from quadratic to linear.

## Transcript

daily temperatures and asks, "For each day, count how many days until the next Now, your instinct might be to pick each day and then just scan forward until a warmer day appears. That does work, but then they say that the input is maybe a
million days, and that O of N squared solution is going to take a trillion See, you can actually do this in a single pass. All you need is a stack of indices for the days still waiting on a warmer
So, when a warmer day does show up, every colder day on the stack just found its answer. You now know exactly how long each one waited. So, you pop them off and you record the gap for each. Let's quickly trace it on an example.
The first three days only get cooler, so each one pushes its index and waits. The each one pushes its index and waits. The stack now holds 0, 1, and 2, decreasing from bottom to top. 72 is warmer than the 69 on top. So, you
1. It's still warmer than the 71 now on It's still warmer than the 71 now on top, so pop index 1 and record 3 - 1, which is 2. One warmer day just cleared two waiting days.
Every index gets pushed once and popped once, so that previous O of N squared scan now collapses into one linear sweep. element to the right, reach for a monotonic stack.
Follow us for more coding interview breakdowns at hellointerview.com.
