Editor

Wait for a Warmer Day

#103 · Stacks and queues · Intermediate · 80 XP · about 15 min

You have a forecast of daily temperatures. For each day, how many days do you have to wait for a day that is strictly warmer?

Write days_until_warmer(temps) that returns a list of the same length, where position i holds the number of days from day i to the next warmer day, or 0 if no later day is warmer.

Examples

days_until_warmer([18, 20, 19, 17, 22, 21, 23])  # returns [1, 3, 2, 1, 2, 1, 0]
days_until_warmer([30, 30, 30])                  # returns [0, 0, 0]
days_until_warmer([5, 10, 15])                   # returns [1, 1, 0]

In the first example day 1 is 20 degrees, and the next warmer day is day 4 at 22 degrees, three days later.

Constraints

  • 0 <= len(temps) <= 100,000
  • -50 <= temps[i] <= 50

Note

A day with the same temperature does not count as warmer.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.