AceYourInterviews
Checking sign-in…

44. Daily Temperatures

IntermediateMonotonic unresolved-index stack

For each day, find the first later day with a higher temperature and report how many days away it is. Report zero if no later day is warmer.

An equal temperature is not warmer. The result is a distance in days, rather than the warmer temperature or its absolute index.

Example 1

Input

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

Output

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

Explanation: Day 2 waits until day 6, while days 6 and 7 have no warmer future day.

Example 2

Input

[70,70,71]

Output

[2,1,0]

Explanation: Equal temperatures remain unresolved until 71 arrives.

Constraints

  • 0 <= temperatures.length <= 105
  • -231 <= temperatures[i] <= 231 - 1
Hints
Hint 1

Keep earlier positions whose answer is still unknown.

Hint 2

Which unresolved positions can today answer?

Hint 3

Pop every cooler top, assign its distance, then push today.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

One day resolves several

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