Daily Temperatures
The problem
Section titled “The problem”Given a list of daily temperatures, return a list where each entry is the number of days
you must wait after that day to see a strictly warmer temperature. If it never gets
warmer, the entry is 0.
[73, 74, 75, 71, 69, 72, 76, 73] -> [1, 1, 4, 2, 1, 1, 0, 0]Day 2 (75) waits 4 days for day 6 (76).
Key insight
Section titled “Key insight”Brute force looks right from each day until it finds a warmer one, which is O(n²) on a
sequence like [90, 80, 70, ...]. The waste is that many days are waiting for the same
warmer day, and we rescan for each.
Flip the view: walk left to right and keep a stack of indices of days that are still
waiting. Their temperatures never increase from bottom to top, because a warmer day
would have resolved the colder ones. When today is warmer than the day on top, that
day’s wait is over: pop it and record i - j. Keep popping while today beats the top,
then push today.
Show solutionHide solution
def daily_temperatures(temperatures: list[int]) -> list[int]: answer = [0] * len(temperatures) stack = [] # indices of days still waiting for a warmer day; temps decrease for i, temp in enumerate(temperatures): while stack and temperatures[stack[-1]] < temp: j = stack.pop() answer[j] = i - j stack.append(i) return answer # days left on the stack never see a warmer day: stay 0export function dailyTemperatures(temperatures: number[]): number[] { const answer: number[] = new Array(temperatures.length).fill(0); const stack: number[] = []; // indices of days still waiting for a warmer day; temps decrease for (let i = 0; i < temperatures.length; i++) { while (stack.length > 0 && temperatures[stack[stack.length - 1]] < temperatures[i]) { const j = stack.pop()!; answer[j] = i - j; } stack.push(i); } return answer; // days left on the stack never see a warmer day: stay 0}import java.util.ArrayDeque;import java.util.Deque;
class Solution { public int[] dailyTemperatures(int[] temperatures) { int[] answer = new int[temperatures.length]; Deque<Integer> stack = new ArrayDeque<>(); // indices still waiting for a warmer day; temps decrease for (int i = 0; i < temperatures.length; i++) { while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) { int j = stack.pop(); answer[j] = i - j; } stack.push(i); } return answer; // days left on the stack never see a warmer day: stay 0 }}Complexity
Section titled “Complexity”- Time: O(n) — the
whileloop looks nested, but every index is pushed once and popped at most once. - Space: O(n) — the stack (and the output) can hold every index, for example with falling temperatures.
Watch out for
Section titled “Watch out for”- Store indices, not temperatures. You need the index to compute the distance and to write into the answer slot.
- Strictly warmer. Use
<, not<=. Equal temperatures stay on the stack and wait. - Leftovers are zeros. Days still on the stack at the end never saw a warmer day, and
the answer array is pre-filled with
0, so there is nothing more to do.
Variations
Section titled “Variations”Next Greater Element I (LeetCode 496)
Section titled “Next Greater Element I (LeetCode 496)”For each value in nums1 (a subset of nums2), find the next greater value to its right
in nums2. The trick: run the same monotonic stack over nums2, storing results in
a map from value to next greater. Then look up each nums1 value.
Online Stock Span (LeetCode 901)
Section titled “Online Stock Span (LeetCode 901)”For each incoming price, return how many consecutive previous days (including today) had
a price at or below it. The trick: a monotonic stack of (price, span) pairs. Pop
every price <= today’s, adding their spans to today’s span, then push.
Largest Rectangle in Histogram (LeetCode 84)
Section titled “Largest Rectangle in Histogram (LeetCode 84)”Find the largest rectangle in a bar chart. The trick: keep a stack of indices with increasing heights. When a shorter bar arrives, pop taller bars; each popped bar’s width runs from the new stack top to the current index. A sentinel bar of height 0 at the end flushes the stack.