Medium
Dec 22, 2025#stack
739. Daily Temperatures
Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.
Example 1:
Example 2:
Example 3:
- 1 <= temperatures.length <= 105
- 30 <= temperatures[i] <= 100
Constraints:
Notes
- Intuition: Store the temperature until a greater temperature is found.
- Implementation: Initialize result with all zeros, we will add in values when we find the larger one. Use a stack to store both temp and index, iterate, while stack is not empty and stack top temp is less than curr, pop from stack, calculate distance with indices, add to result at popped index. Add curr temp and index to stack.
- Complexity: Time O(n), Space O(n) (stack)