iq.lab
Python starts when a code cell comes near or you run one
mediumMonotonic stack target 25 min

Daily temperatures

A list temps holds one temperature reading per day, in day order. For each day, look ahead for the first later day with a strictly higher reading, and report how far ahead it is: 1 if it is the next day, 2 if it is the day after that, and so on. A reading equal to that day's does not count. If no later reading is higher, report 0.

Return a list of these numbers, one per day, in day order.

Example 1
Inputtemps = [70, 68, 72, 71, 75]Output[2, 1, 2, 1, 0]

Day 0 (70) waits until day 2 (72), which is 2 days. Day 2 (72) waits until day 4 (75). Nothing comes after day 4.

Example 2
Inputtemps = [50, 40, 50, 60]Output[3, 1, 1, 0]

Day 2 is as warm as day 0, not warmer, so day 0 waits until day 3.

Example 3
Inputtemps = [65, 60, 55]Output[0, 0, 0]

It only gets colder, so no day ever sees a warmer one.

Constraints
  • 1 ≤ len(temps) ≤ 105

  • 30 ≤ temps[i] ≤ 100

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.