Practice

Daily Temperatures

Module 8 · Stacks

Problem

Given daily temperatures, return an array where answer[i] is the number of days you must wait after day i for a warmer temperature — 0 if it never comes.

Examples

Example 1

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

Example 2

Input[30,40,50,60]Output[1,1,1,0]

Example 3

Input[30,60,90]Output[1,1,0]

Constraints

1 ≤ n ≤ 10⁵ · temperatures in [30, 100].

Attempt it first

Read the question as the monotonic-stack lesson taught: this is next-greater-element with the answer expressed as a distance (i − j) instead of a value. If you can adapt the template without re-opening the lesson, the pattern is yours. One extra wrinkle: which flavor row, and what exactly gets recorded on each pop?