Skip to main content

Command Palette

Search for a command to run...

LeetCode: Daily Temperatures

Updated
4 min read

Problem:

https://leetcode.com/problems/daily-temperatures/

Code:

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        stack=[]
        n=len(temperatures)
        answer=[0]*n

        for i in range(n):
            while stack and temperatures[stack[-1]]<temperatures[i]: # loop till it finds a warmer temperature
                index=stack.pop()
                answer[index]=i-index
            stack.append(i)
        return answer

Key Points:

Core Idea (Think of "waiting list" for warmer days)

We want to know: for each day, how many days do we wait until a warmer temperature? We keep a stack that stores the indices of days still waiting for a warmer day.

Why Stack Works

  • Temperature on stack top is always the most recent still-waiting day.

  • If a new day is warmer → it’s the answer for those older colder days.

  • We can quickly connect a warmer day to the closest colder day before it.

Expanding Through Days (Left → Right)

For each day i:

  • If current temperature is warmer than the temperature of the day on top of stack: → Pop that older day → Calculate wait time: (i - popped_day_index) → Store in the answer

  • Keep doing this while current day is warmer

  • Then push current day index on stack (it also waits for future warmth)

Why Do We Pop?

  • A warmer day finally came → no need to keep waiting

  • We remove it from stack because its job is done

What About Days That Never Get Warmer?

  • We leave them in stack

  • Their answer stays 0 (already set during initialization)

Final Answer

A list where each spot tells: “How many days must this day wait to see a warmer temperature?” (or 0 if no future day is warmer)

Why This Works and Is Fast

  • Each day’s index is pushed and popped only once

  • So instead of comparing with every other day, we do the minimum number of comparisons needed

Complexity

Time: O(n) → linear scan with smart jumps
Space: O(n) → stack stores unresolved days

Simple Visualisation:

Day index:      0    1    2    3    4    5    6    7
Temperature:   73   74   75   71   69   72   76   73
                 │    │    │    │    │    │    │
                 │    │    │    │    │    │    └── no warmer day → 0
                 │    │    │    │    │    └──────→ waits 1 day for 76 (day 6)
                 │    │    │    │    └───────────→ waits 1 day for 72 (day 5)
                 │    │    │    └────────────────→ waits 2 days for 72 (day 5)
                 │    │    └─────────────────────→ waits 4 days for 76 (day 6)
                 │    └──────────────────────────→ waits 1 day for 75 (day 2)
                 └───────────────────────────────→ waits 1 day for 74 (day 1)

Dry Run:

Day (i)TempStack BeforeCondition CheckAction TakenUpdated AnswerStack After
073[]Push 0[0,0,0,0,0,0,0,0][0]
174[0]74 > 73 → TruePop 0 → answer[0] = 1−0 = 1, Push 1[1,0,0,0,0,0,0,0][1]
275[1]75 > 74 → TruePop 1 → answer[1] = 2−1 = 1, Push 2[1,1,0,0,0,0,0,0][2]
371[2]71 > 75 → FalsePush 3[1,1,0,0,0,0,0,0][2,3]
469[2,3]69 > 71 → FalsePush 4[1,1,0,0,0,0,0,0][2,3,4]
572[2,3,4]72 > 69 → TruePop 4 → answer[4] = 5−4 = 1[1,1,0,0,1,0,0,0][2,3]
[2,3]72 > 71 → TruePop 3 → answer[3] = 5−3 = 2[1,1,0,2,1,0,0,0][2]
[2]72 > 75 → FalsePush 5[1,1,0,2,1,0,0,0][2,5]
676[2,5]76 > 72 → TruePop 5 → answer[5] = 6−5 = 1[1,1,0,2,1,1,0,0][2]
[2]76 > 75 → TruePop 2 → answer[2] = 6−2 = 4[1,1,4,2,1,1,0,0][]
[]Push 6[1,1,4,2,1,1,0,0][6]
773[6]73 > 76 → FalsePush 7[1,1,4,2,1,1,0,0][6,7]