stacks-queues · Problem 2 of 2

Daily Temperatures

medium
Amazon logoAmazon
Google logoGoogle

For each day, return how many days you must wait for a warmer temperature. Use 0 where none comes.

dailyTemperatures([73, 74, 75, 71, 69, 72, 76, 73])
  ->  [1, 1, 4, 2, 1, 1, 0, 0]

Your solution

Runs your code and animates it without grading anything. Change the input to see what it does on a case the tests do not cover.

Running is free — Submit is what records it. Or press ⌘↩

Tests

4 cases, 1 hidden
calltypeexpectedresult
dailyTemperatures([73,74,75,71,69,72,76,73])classic[1,1,4,2,1,1,0,0]
dailyTemperatures([30,40,50,60])increasing[1,1,1,0]
dailyTemperatures([50,40,30])decreasing[0,0,0]
withheldhiddenwithheld

Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.

Complexity

target time
O(n)
target space
O(n)

A monotonic stack. Each index is pushed once and popped once, so it is linear despite the nested while.

Hints

Stuck? Hints open one at a time, each giving a little more away.

3 hints left