Menu
CoddyTech

Daily Temperatures

You get the temperature of each day in a row of days: temperatures[i] is the temperature on day i. For every day, count how many days you have to wait after it until a strictly warmer day arrives. If no warmer day comes later, the wait for that day is 0.

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 is 71 and the first warmer day is day 2 at 72, so it waits 2 days. Days 3 and 4 are both 70: the second 70 is not warmer, so day 3 waits until day 5 at 75, which is 2 days. Nothing after 75 or 68 is warmer, so both get 0.

lock icon+13 hidden tests on Submit

challenge icon

Follow-up

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

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

Case 1

Case 2

Case 3

Input

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

Expected

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