LeetCode 739 Daily Temperatures - Solution

The core idea

Use a monotonic decreasing stack that holds indices of days waiting for a warmer temperature.

Process temperatures left to right. For each day:

  • While the stack isn't empty and today's temperature is warmer than the day at stack top, pop that index. The wait time is today - poppedIndex.
  • Push today's index onto the stack.

Days remaining on the stack never found a warmer day, so their answer stays 0.

Each index is pushed once and popped at most once.