Daily Temperatures

Medium

Topics
ArrayStackMonotonic Stack

Given an array temperatures, return an array answer where answer[i] is the number of days you must wait after day i for a warmer temperature. If none, put 0.

Example 1

nums730741752713694725766737
answer11421100
index 0its next greater value
For each cell the answer is about the first later value greater than it.
Top row is the input; bottom row is the answer per index, as a wait in days (0 when none comes).
Input:  temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]

Example 2

nums300401502603
answer1110
index 0its next greater value
Input:  temperatures = [30,40,50,60]
Output: [1,1,1,0]

Constraints

  • 1 <= temperatures.length <= 10^5
  • 30 <= temperatures[i] <= 100
Editorial — intuition, approach, complexity and pitfalls

Intuition

Keep a stack of days that are still "waiting" for a warmer day, with temperatures decreasing down the stack. When a warmer day arrives it resolves every colder day on top.

Approach

  1. Iterate with a stack of indices.
  2. While the current temp exceeds the temp at the stack top, pop it and record the day gap.
  3. Push the current index.

Dry Run

temps = [73, 74, 75, 71, 69, 72, 76, 73]
The stack holds INDICES of days still waiting, and temperatures decrease from the
bottom of the stack to the top.
  i=0  73   nothing to resolve                             stack [0]
  i=1  74   resolves day 0:  1 - 0 = 1                     stack [1]
  i=2  75   resolves day 1:  2 - 1 = 1                     stack [2]
  i=3  71   colder than 75, so it waits as well            stack [2, 3]
  i=4  69   colder still                                   stack [2, 3, 4]
  i=5  72   resolves day 4 (69) -> 1
            resolves day 3 (71) -> 2
            then stops: 75 is warmer than 72, day 2 waits  stack [2, 5]
  i=6  76   resolves day 5 (72) -> 1
            resolves day 2 (75) -> 4                       stack [6]
  i=7  73   colder than 76, waits                          stack [6, 7]
answer 1 1 4 2 1 1 0 0
Days 6 and 7 are still on the stack when the loop ends and their answers stay 0,
which needs no handling at all because res was initialised to zeros.
i=5 is the row that shows why this is O(n) rather than O(n^2). One day resolves
two earlier days at once, and because the stack is decreasing the inner loop can
stop the instant it meets something warmer -- day 2 is not looked at again until
i=6. Every index is pushed once and popped at most once.
The stack stores indices, not temperatures, because the answer is a DISTANCE. The
temperature is only the comparison key; i - j is the thing being asked for.
The pop test is strictly <. Make it <= and a day gets resolved by a later day of
the same temperature, which is not warmer: on [3,1,0,1,2] the correct answer is
0 3 1 1 0 and <= gives 0 2 1 1 0.

Complexity

  • Time — O(n): each index is pushed and popped at most once.
  • Space — O(n): the stack.

Common Pitfalls

  • Pop on strictly warmer only. Using <= resolves a day against a later day of the same temperature, which is not warmer: on [3,1,0,1,2] the answer becomes 0 2 1 1 0 where it should be 0 3 1 1 0.
  • The stack holds indices, not temperatures. The answer is a distance, so the position is the thing you need and the value is only the comparison key.

Full Solution — all languages

class Solution:
    def dailyTemperatures(self, nums):
        res=[0]*len(nums)
        stack=[]
        for i,t in enumerate(nums):
            while stack and nums[stack[-1]]<t:
                j=stack.pop(); res[j]=i-j
            stack.append(i)
        return res
SensAI
Beta
Tap the mic to stop voice mode