Watch 10 video solutions for Daily Temperatures, a medium level problem involving Array, Stack, Monotonic Stack. This walkthrough by NeetCode has 348,736 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0]
Example 2:
Input: temperatures = [30,40,50,60] Output: [1,1,1,0]
Example 3:
Input: temperatures = [30,60,90] Output: [1,1,0]
Constraints:
1 <= temperatures.length <= 10530 <= temperatures[i] <= 100Problem Overview: You receive an array where temperatures[i] represents the temperature on day i. For every day, compute how many days you must wait until a warmer temperature appears. If no warmer day exists, return 0 for that position.
Approach 1: Monotonic Stack (O(n) time, O(n) space)
The optimal solution uses a monotonic decreasing stack that stores indices of days whose warmer temperature hasn't been found yet. As you iterate through the array, compare the current temperature with the temperature at the index on top of the stack. If the current temperature is higher, you've found the next warmer day for that index. Pop the index and compute the distance currentIndex - previousIndex. Continue until the stack is empty or the top temperature is greater than or equal to the current one. Push the current index afterward.
This works because each index is pushed and popped at most once, keeping the algorithm linear. The stack maintains a decreasing sequence of temperatures, which guarantees that when a warmer day appears it resolves multiple previous days efficiently. This is a classic use of stack combined with the monotonic stack pattern commonly used in "next greater element" style problems.
Approach 2: Optimized Array Traversal (O(n) time, O(n) space)
Another strategy scans the array from right to left while using previously computed answers to skip unnecessary checks. Maintain a result array initialized with zeros. For each index i, jump forward using i + result[i] until you find a warmer temperature or reach the end. If temperatures[j] is warmer, store j - i. If not, keep jumping using previously stored answers.
This method avoids an explicit stack but still leverages the idea that future results are already computed. In practice, it behaves similarly to dynamic programming over an array. Each index is visited a limited number of times, keeping overall complexity linear. The approach is useful in languages where array operations are simple and predictable.
Recommended for interviews: The monotonic stack solution is the expected answer. Interviewers often use this problem to test whether you recognize the "next greater element" pattern and can apply a monotonic structure efficiently. Brute force reasoning (checking future days) demonstrates baseline understanding, but implementing the O(n) stack solution shows strong algorithmic pattern recognition.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Monotonic Stack | O(n) | O(n) | Best general solution. Ideal for next greater element style problems. |
| Optimized Array Traversal | O(n) | O(n) | When scanning from right to left and reusing computed results is easier than maintaining a stack. |