Practice · Monotonic stack · 1 of 2

Days Until a Warmer Day

You are given temperatures, the temperature on each of a run of days, in order.

Return a list answer of the same length, where answer[i] is how many days after day i the first strictly warmer day comes. If no later day is warmer, answer[i] is 0.

Example 1

Input
temperatures = [70, 68, 72, 71, 69, 75]
Output
[2, 1, 3, 2, 1, 0]
Day 0 (70) waits until day 2 (72); day 2 (72) waits until day 5 (75); nothing comes after day 5.

Example 2

Input
temperatures = [50, 50, 50]
Output
[0, 0, 0]
Only a strictly warmer day counts, so equal days never answer each other.

Constraints

  • temperatures holds 1 to 10⁵ values.
  • Every temperature is between 30 and 100.