Understanding Daily Temperatures
For each daily temperature, return how many days you must wait for a strictly warmer day. Return zero if no later day is warmer.
How it works
A stack of unresolved day indices lets a warm day answer several earlier days at once.
Maintain a stack with non-increasing temperatures. Pop and resolve earlier colder days before pushing the current day.
Try an example
Input: temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
Output: [1, 1, 4, 2, 1, 1, 0, 0]
Day 2 waits four days to reach temperature 76.
Constraints
- At most 64 temperatures between −100 and 100.
- An equal temperature is not warmer.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.