Stacks and queues · Problem 2 of 3

Days until a warmer day

Pattern: Daily Temperatures: Monotonic Stack

mediumNot solved yet

The problem

You have a list of daily temperatures. For each day, work out how many days you'd wait until a strictly warmer day. If no warmer day comes later, use 0. Return a list with one number per day.

text
1days_until_warmer([18, 17, 21, 19, 19, 24])  ->  [2, 1, 3, 2, 1, 0]
2days_until_warmer([30, 25, 20])              ->  [0, 0, 0]
3days_until_warmer([5, 6, 7])                 ->  [1, 1, 0]
Example 1
012345
temp181721191924
wait213210

Day 0 (18°) waits 2 days for the 21° on day 2.

Write days_until_warmer(temps) in Python or daysUntilWarmer(temps) in JavaScript.

Runs privately in your browser. Nothing is sent to a server.Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Write your function, then run it against 20 tests.

Breakdown

Stuck? Reveal one step at a time, and try again after each one.

  1. Step 2
  2. Step 3
  3. Step 4

Solution

?

Give it a real try first: you learn far more from a wrong attempt than from reading the answer.