739. Daily Temperatures
Asked at Google, Meta, Amazon, Microsoft and 4 more
Difficulty
Medium
Time
O(n)
Space
O(n)
Pattern
Stack
Companies
8
Problem
You are given an array temperatures, where temperatures[i] is the temperature on day i. For each day, you want to know how many days you must wait until a strictly warmer day arrives.
Return an array answer of the same length. answer[i] is the number of days between day i and the next day whose temperature is higher than temperatures[i]. If no warmer day ever comes, set answer[i] = 0.
Note that "warmer" means strictly greater. A day with the same temperature does not count, so you keep looking further ahead.
How many days until it gets warmer
Constraints
- ●
1 <= temperatures.length <= 10^5 - ●
30 <= temperatures[i] <= 100
Examples
temperatures = [73,74,75,71,69,72,76,73][1,1,4,2,1,1,0,0]temperatures = [30,40,50,60][1,1,1,0]temperatures = [30,60,90][1,1,0]Asked At
8 companies| Company | Difficulty | |
|---|---|---|
| Medium | View all Google questions → | |
| Meta | Medium | View all Meta questions → |
| Amazon | Medium | View all Amazon questions → |
| Microsoft | Medium | View all Microsoft questions → |
| Apple | Medium | View all Apple questions → |
| Uber | Medium | View all Uber questions → |
| Goldman Sachs | Medium | View all Goldman Sachs questions → |
| Anduril | Medium | View all Anduril questions → |
How to Think About It
Brute force checks every future day for each day — O(n²). The monotonic stack approach brings it to O(n).
Use a monotonic decreasing stack storing indices (not values). The stack holds days that haven't found a warmer day yet.
When you encounter a warmer day, pop all cooler days from the stack and record the distance. Each index is pushed and popped at most once.
Why it works: the stack maintains days in decreasing temperature order. A warmer day "resolves" all previous cooler days.
Edge cases: all same temperature (all zeros), strictly increasing (all ones), strictly decreasing (all zeros except last).
Optimal Approach
Step 1: Create result array of size n, fill with 0.
Step 2: Use a stack to store indices.
Step 3: For each day i:
While stack is not empty and temperatures[i] > temperatures[stack[-1]]:
j = stack.pop()
result[j] = i - j
Push i onto stack.
Step 4: Remaining indices in stack already have result[j] = 0.
Each index is pushed once and popped once, giving O(n) time.
Time: O(n). Space: O(n).
Monotonic stack of waiting days
What Trips People Up in Real Interviews
Using brute force O(n²). The monotonic stack approach is O(n) and is what the interviewer expects.
Storing values in the stack instead of indices. You need indices to compute the answer (difference in positions).
Not handling the case where no warmer day exists. The answer for that day is 0 (already initialized).
Forgetting that the stack might not be empty after the loop. Remaining indices already have result = 0.
Using a monotonic INCREASING stack instead of decreasing. You need a decreasing stack so that when you find a warmer day, it resolves all previous cooler days. An increasing stack never pops.
Solution Code
O(n) timedef dailyTemperatures(temperatures):
n = len(temperatures)
result = [0] * n
stack = []
for i, temp in enumerate(temperatures):
while stack and temperatures[stack[-1]] < temp:
j = stack.pop()
result[j] = i - j
stack.append(i)
return resultFrequently Asked Questions
What is the Daily Temperatures problem?
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.
How do you solve Daily Temperatures?
The optimal approach runs in O(n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Daily Temperatures?
Daily Temperatures is asked at Google, Meta, Amazon, Microsoft, Apple, Uber, Goldman Sachs, Anduril. It is a medium difficulty problem.
What are common mistakes on Daily Temperatures?
- Using brute force `O(n²)`. The monotonic `stack` approach is `O(n)` and is what the interviewer expects.
- Storing values in the `stack` instead of indices. You need indices to compute the answer (difference in positions).
- Not handling the case where no warmer day exists. The answer for that day is 0 (already initialized).
- Forgetting that the stack might not be empty after the loop. Remaining indices already have `result = 0`.
- Using a monotonic INCREASING `stack` instead of decreasing. You need a decreasing `stack` so that when you find a warmer day, it resolves all previous cooler days. An increasing `stack` never pops.