Use a monotonic decreasing stack of indices.
Scan temperatures. While the current temperature is higher than the temperature at the stack top, pop that index and set the answer as the distance to the current day.
Each index stays on the stack until a warmer day appears; the first warmer day is exactly when it gets popped.
Use a monotonic decreasing stack of indices.
Scan temperatures. While the current temperature is higher than the temperature at the stack top, pop that index and set the answer as the distance to the current day.
Each index stays on the stack until a warmer day appears; the first warmer day is exactly when it gets popped.