all entries
A-025Day 252026.05.29mediumleetcode #739neetcode150

Daily Temperatures

#array#stack#monotonic-stack
leetcode #739 · daily-temperatures
01

Problem

· problem
P.025

Daily Temperatures

leetcode #739

Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.

constraints
  • · 1 ≤ temperatures.length ≤ 10^5
  • · 30 ≤ temperatures[i] ≤ 100
// paraphrased summary — see source for full text
examples
example 1input → output
[73,74,75,71,69,72,76,73]
[1,1,4,2,1,1,0,0]
example 2input → output
[30,40,50,60]
[1,1,1,0]
example 3input → output
[30,60,90]
[1,1,0]
02

Pre-solve

· pre-solve
● 1list shown● 2select● 3reveal
  • If the next day's temperature equals the current day's, is that considered 'warmer'?
  • If no warmer day exists, what should answer[i] contain?
  • Does answer[i] contain the warmer temperature's value or the number of days to wait?
  • Can we assume every day is guaranteed to eventually have a warmer day?
  • Must we process temperatures strictly left-to-right?
  • Is a naive scan checking all future days for each day sufficient?
  • Can the same temperature value appear multiple times in the array?
check the items you would ask, then press confirm
// session-only state — refresh resets (repeatable practice)
03

Logic Structure

· logic
● 1slots shown● 2pick per slot● 3reveal
// pick one code line per slot to assemble the algorithm flow. no typing — just the logic skeleton.
step 1· Initialize Result Array
res = [0] * len(temperatures)
res = []
res = [-1] * len(temperatures)
step 2· Initialize Stack
stack = []  # pair: [temp, index]
stack = None
stack = {}
step 3· Main Loop
for i, t in enumerate(temperatures):
for t in temperatures:
for i in range(len(temperatures)-1, -1, -1):
step 4· Pop Conditionnested
while stack and t > stack[-1][0]:
while t > stack[-1][0]:
while stack and t >= stack[-1][0]:
step 5· Pop from Stack│ │ nested
stackT, stackInd = stack.pop()
stackInd, stackT = stack.pop()
stackT = stack.pop()[0]
step 6· Record Answer│ │ nested
res[stackInd] = i - stackInd
res[i] = i - stackInd
res[stackInd] = i
step 7· Append to Stacknested
stack.append((t, i))
stack.append(t)
stack.insert(0, (t, i))
pick one option per slot
// format: slot — recursive / DP patterns use ordering / state-first formats. ADR-08 follow-up.
04

Solve · Trace

· solve
solution.py
1
class Solution:
2
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
3
        res = [0] * len(temperatures)
4
        stack = []  # pair: [temp, index]
5
6
        for i, t in enumerate(temperatures):
7
            while stack and t > stack[-1][0]:
8
                stackT, stackInd = stack.pop()
9
                res[stackInd] = i - stackInd
10
            stack.append((t, i))
11
        return res
mental dry-run cases
// walk each case in your head; expand the worked example below if stuck.
case 1
[73,74,75,71,69,72,76,73]
[1,1,4,2,1,1,0,0]
case 2
[30,40,50,60]
[1,1,1,0]
case 3
[30,60,90]
[1,1,0]
// UI does not walk-through — you do the dry-run mentally. Expand the worked example if stuck.