Skip to contentKEEP THE CRAFTTHE SANCTUARY

Daily Temperatures

For each day, count the days until a warmer one with a monotonic stack of indices.

The problem

Implement Solution.dailyTemperatures(temperatures). temperatures[i] is the temperature on day i. Return a list answer where answer[i] is the number of days you have to wait after day i to get a strictly warmer temperature, or 0 if no later day is warmer. For example, [73, 74, 75, 71, 69, 72, 76, 73] gives [1, 1, 4, 2, 1, 1, 0, 0]: after day 2 (75), the next warmer day is day 6 (76), four days later. An equal temperature does not count as warmer. Constraints: 1 to 100,000 days, each temperature from 30 to 100.

Starter code

class Solution:
    def dailyTemperatures(self, temperatures):
        pass

Public examples

  1. temperatures = [73, 74, 75, 71, 69, 72, 76, 73]gives[1, 1, 4, 2, 1, 1, 0, 0]
  2. temperatures = [30, 40]gives[1, 0]
  3. temperatures = [30, 40, 35]gives[1, 0, 0]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.