MediumArrayStackMonotonic Stack

1 approach, code in every language

Problem description

You are given an array `temperatures` where `temperatures[i]` is the temperature recorded on day `i`.

For each day, determine how many days you must wait until a warmer temperature occurs. If no future day is warmer, the answer for that day is `0`.

Return an array `answer` such that `answer[i]` is the number of days to wait after day `i` for a warmer temperature.

Example 1

Input: temperatures = [73,74,75,71,69,72,76,73]

Output: [1,1,4,2,1,1,0,0]

Day 0 (73) warms up the next day (74), so 1. Day 2 (75) waits 4 days for 76. The last two days never see anything warmer, so 0.

Example 2

Input: temperatures = [30,40,50,60]

Output: [1,1,1,0]

Each day is immediately followed by a warmer one except the final day, which has no warmer future day.

Constraints

  • 1 <= temperatures.length <= 10^5
  • 30 <= temperatures[i] <= 100

After the pattern

Now solve one you haven't seen.

Nights Until a Fuller House is our own problem on the technique behind Daily Temperatures: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free