32. Daily Temperatures

For each day, how many days do you have to wait until a warmer temperature? Use 0 if no warmer day follows.

Example 1

Input: temperatures = [30,38,32,35,40,31]
Output: [1,3,1,1,0,0]

Example 2

Input: temperatures = [50,50,50]
Output: [0,0,0]
Hint 1

This is the “next greater element” pattern.

Hint 2

Keep a stack of days that have not found a warmer day yet; their temperatures are decreasing.

Monotonic Stack