Skip to the content

Days until warmer

Problem

"How long until it gets warmer?" the High Valley weather station hears every morning, and it wants to answer that for every day in its log.

You get the list of temperatures, one per day (whole numbers, they can be negative; the list may be empty). Return a list of the same length: for each day, how many days you have to wait until the first one that is warmer than it, or 0 if no such day comes after it.

It has to be warmer: an equal day does not count. And it is the first one that beats it, not the warmest one. The last day always gives 0, and an empty list returns [].

With [20, 18, 22, 21, 25]: the 20 is beaten by the 22, two days later; the 18, by the 22 the next day; the 22 and the 21, by the 25. You return [2, 1, 2, 1, 0].

Examples

  • The example

    [20, 18, 22, 21, 25] → [2, 1, 2, 1, 0]

  • A week and a day

    [73, 74, 75, 71, 69, 72, 76, 73] → [1, 1, 4, 2, 1, 1, 0, 0]

  • Colder every day

    [30, 25, 20] → [0, 0, 0]

  • Warmer every day

    [1, 2, 3, 4] → [1, 1, 1, 0]

  • The first one that beats it, not the warmest

    [10, 12, 20, 11] → [1, 1, 0, 0]

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def wait_days(temps):
    pass

JavaScript

function waitDays(temps) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →