Daily Temperatures

Medium Monotonic Stack Monotonic Stack Original on LeetCode

Daily Temperatures is a medium monotonic stack problem solved with the monotonic stack pattern. The best approach, optimal (monotonic stack of indices), runs in O(n) time and O(n) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force up.

Problem

Given daily temperatures, return an array where answer[i] is the number of days until a warmer temperature after day i. If no warmer day comes, use 0.

Examples

Example 1

Input
temperatures = [30, 32, 31, 29, 35, 30]
Output
[1, 3, 2, 1, 0, 0]
Why
Day 1 (32) waits 3 days for 35.

Example 2

Input
temperatures = [50, 40, 30]
Output
[0, 0, 0]

Constraints

  • 1 <= temperatures.length <= 10^5
  • 30 <= temperatures[i] <= 100

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Brute forceO(n²)O(1)
Optimal (monotonic stack of indices)O(n)O(n)

1Brute force

TimeO(n²)
SpaceO(1)

For each day, scan forward to the first warmer day.

  1. For each i, find the smallest j > i with t[j] > t[i]; answer j - i.
class Solution {
    public int[] dailyTemperatures(int[] t) {
        int n = t.length;
        int[] out = new int[n];
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
                if (t[j] > t[i]) { out[i] = j - i; break; }
        return out;
    }
}

2Optimal (monotonic stack of indices)

TimeO(n)
SpaceO(n)

Keep a stack of indices of days still waiting for a warmer day, with decreasing temperatures. A warmer day pops every cooler day on top and fills in the distance.

  1. For each i: while t[stack top] < t[i], j = pop(); out[j] = i - j.
  2. Push i.
class Solution {
    public int[] dailyTemperatures(int[] t) {
        int[] out = new int[t.length];
        Deque<Integer> st = new ArrayDeque<>();
        for (int i = 0; i < t.length; i++) {
            while (!st.isEmpty() && t[st.peek()] < t[i]) {
                int j = st.pop();
                out[j] = i - j;
            }
            st.push(i);
        }
        return out;
    }
}

Edge cases to test

  • Equal temperatures are not warmer
  • Strictly decreasing input

Hints

Hint 1

Store indices, not temperatures, on the stack so you can compute the distance.

FAQ

What is the best time complexity for Daily Temperatures?

Optimal (monotonic stack of indices) runs in O(n) time and O(n) extra space.

Which pattern does Daily Temperatures use?

It is a monotonic stack problem that uses the monotonic stack pattern. Other problems with the same pattern: Next Greater Element I, Next Greater Element II, Largest Rectangle in Histogram.

Is there a brute force solution for Daily Temperatures?

Yes. Brute force takes O(n²) time and O(1) space. For each day, scan forward to the first warmer day.

Which edge cases should I test for Daily Temperatures?

Equal temperatures are not warmer; Strictly decreasing input.