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
Input
temperatures = [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.
Input
temperatures = [30, 40, 50, 60]Output
[1, 1, 1, 0]Strictly rising: every day is answered by tomorrow.
Input
temperatures = [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.