A-025● Day 252026.05.29mediumleetcode #739neetcode150
Daily Temperatures
#array#stack#monotonic-stack
01
Problem
· problemGiven 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
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 Condition│ nested
○
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 Stack│ nested
○
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
· solvemental 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.