Wait Time Until a Higher Temperature Arrives

Implement daysUntilWarmer

Given a list of daily temperature readings temps, find, for every day, how many days it takes until a strictly warmer day arrives — or 0 if it never warms up again before the record ends. This is the "next greater element" pattern, but asking for the distance to the answer instead of the value itself. A monotonic decreasing stack of day indices (not temperatures) makes the distance trivial to compute: whenever a warmer day arrives, every colder day still waiting on the stack gets popped, and its wait time is simply the gap between the current day's index and the popped day's index. Every day enters and leaves the stack at most once, giving O(n) total work instead of the brute force's O(n²) repeated scanning.

Example 1:

Input: temps = [70,68,72,71,74]

Output: [2,1,2,1,0]

Example 2:

Input: temps = [88,85,90]

Output: [2,1,0]

Example 3:

Input: temps = [50,55,60,65]

Output: [1,1,1,0]

+ 3 hidden test cases run on Submit.

Constraints:

  • 1 ≤ temps.length ≤ 15
  • 30 ≤ temps[i] ≤ 100

temps =

[70, 68, 72, 71, 74]