#739 Daily Temperatures

MediumStack~20 min

You're given an array temperatures, one reading per day. For each day, figure out how many days you'd have to wait until a strictly warmer day arrives. Produce an array answer of the same length where answer[i] is that wait, and 0 if no warmer day ever comes.

Examples

Inputtemperatures = [73, 74, 75, 71, 69, 72, 76, 73]
Output[1, 1, 4, 2, 1, 1, 0, 0]

Day 2 (75) waits 4 days for 76, days 3, 4, and 5 are all colder, so they can't answer it. The last day has nothing after it: 0.

Inputtemperatures = [30, 40, 50, 60]
Output[1, 1, 1, 0]

Strictly rising: every day is answered by tomorrow.

Inputtemperatures = [60, 50, 40, 30]
Output[0, 0, 0, 0]

Strictly falling: nobody ever gets a warmer day.

Constraints

  • 1 ≤ temperatures.length ≤ 10⁵
  • 30 ≤ temperatures[i] ≤ 100

The explainer is the good part.

Sign in for the full intuition build-up, every approach from naive to optimal with complexity analysis, progressive hints, and commented solutions in Python, JavaScript, and Java. All free.

Want a taste first? Try a free sample: Two Sum or Valid Parentheses.