Back to Problem
Official 1884 views

Editorial: Days Until Warmer

by admin ·

14

1 Intuition

Days still waiting for a warmer day form a decreasing stack.

2 Approach

Push indices; when a warmer day arrives, pop every colder day and record the distance.

3 Complexity Analysis

Time O(n), Space O(n).

4 Solution Code

class Solution:
    def dailyTemperatures(self, temperatures: list[int]) -> list[int]:
        out = [0] * len(temperatures)
        stack = []
        for i, t in enumerate(temperatures):
            while stack and temperatures[stack[-1]] < t:
                j = stack.pop()
                out[j] = i - j
            stack.append(i)
        return out

5 Code Walkthrough

The reference solution below implements the approach step by step.