Daily Temperatures

Medium Stack & Queue AI round

temperatures[i] is the temperature on day i, in tenths of a degree: 215 is 21.5 degrees.

For each day, look ahead for the first later day that is warmer: one with a higher temperature. The day's wait is how many days later that warmer day comes.

Return a list of the wait for every day, in order, with 0 for a day that no later day is warmer than.

Example 1

Input: temperatures = [215,198,203,240,187,187,226]
Output: [3,1,1,0,2,1,0]

Day 0 is 21.5 degrees, and the first warmer day is day 3, at 24.0: three days later. Days 4 and 5 are both 18.7, and a day of the same temperature is not warmer, so day 4 waits for day 6, two days later.

Example 2

Input: temperatures = [-30,-30,-30]
Output: [0,0,0]

Equal days are not warmer, so no day gets a warmer one.

Example 3

Input: temperatures = [50,40,30,60]
Output: [3,2,1,0]

Constraints

  • 1 ≤ temperatures.length ≤ 5 × 10⁵
  • -500 ≤ temperatures[i] ≤ 500

Watch the explanation

More Stack & Queue

All Stack & Queue problems