Daily Temperatures

Given an array temperatures where temperatures[i] is the temperature on day i, return an array answer such that answer[i] is the number of days you have to wait after day i to get a warmer temperature. If there is no future day for which this is possible, put 0. Example: Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0] Explanation: Day 0 waits 1 day, day 2 waits 4 days, and the last two days have no warmer future day. Pattern focus: Monotonic Decreasing Stack for Next Greater.

Input Format

temperatures = daily temperatures

Output Format

answer[i] = number of days until a warmer temperature, or 0 if none exists

Constraints

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

Examples

Example 1:

Input:

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

Output:

[1,1,4,2,1,1,0,0]

Explanation:

A decreasing stack of indices lets us answer each day in O(1) amortized time.

Example 2:

Input:

temperatures = [30,40,50,60]

Output:

[1,1,1,0]

Explanation:

Each new warmer day resolves previous colder days.

Loading...
Daily Temperatures - Monotonic Stack Queue