Menu
CoddyTech

Daily Temperatures

temperatures[i] is the temperature on day i. For every day, count how many days you wait after it until a strictly warmer day arrives, or 0 if no warmer day comes later. Return an array of the same length where entry i is the wait for day i.

Function

dailyTemperatures(temperatures: integer-array) → integer-array
temperaturesinteger-array
the temperature of each day, in order
Returnsinteger-array
for each day, the number of days until a warmer one, or 0 if none comes

Constraints

  • 1 ≤ temperatures.length ≤ 104
  • 30 ≤ temperatures[i] ≤ 100
  • Warmer means strictly higher: a later day with the same temperature does not count.

Examples

Input
temperatures = [71, 69, 72, 70, 70, 75, 68]
Output
[2, 1, 3, 2, 1, 0, 0]
Explanation
Day 0 (71) waits 2 days for 72. Day 3 (70) skips the equal 70 on day 4 and waits 2 days for 75. Nothing after 75 or 68 is warmer, so both get 0.
challenge icon

Follow-up

Temperatures take only 71 values, 30 to 100. How could a table indexed by temperature answer every day in one pass from right to left, and at what cost?

Reset code
def dailyTemperatures(temperatures):
    # Write code here

Test #1

Test #2

Test #3

Argument

temperatures = [71, 69, 72, 70, 70, 75, 68]

Expected Output

[2, 1, 3, 2, 1, 0, 0]