Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Daily Temperatures

LeetCode #739Medium
Monotonic stack · new days resolve waiting days

Given an array of daily temperatures, return an array where each entry is the number of days you must wait after that day to encounter a warmer temperature. Use 0 if no warmer day ever follows.

Asked atAmazonGoogleMeta
step 1 / 33
73
74
75
71
69
72
76
73
[0][1][2][3][4][5][6][7]
answer (days to wait)
·
·
·
·
·
·
·
·
[0][1][2][3][4][5][6][7]
Brute force · scan right from every day
1given temps
2for i ← 0 to n − 1:
3 scan j = i+1, i+2, … until temps[j] > temps[i]
4 answer[i] = found ? ji : 0
5return answer
state
  • n8

line 1For each day: how many days until a WARMER temperature? 0 if it never comes. Obvious approach: from each day, scan right.