Home
DSA Patterns
Stack
Daily Temperatures
Daily Temperatures (LeetCode 739)
Daily Temperatures (LeetCode 739): find how many days you have to wait for a warmer temperature.
Problem Understanding
temperatures[i] is the temperature on day i. Build an array answer where answer[i] counts the days from day i until the first strictly warmer day. If no warmer day ever comes, answer[i] stays 0.
Algorithm Strategy
We use a Monotonic Decreasing Stack.
- Initialize
answerarray with 0s. - Use a stack to store indices of days.
- Iterate through
temperatures. - While the current temperature is warmer than the temperature at the index stored in
stack.top():- We found a warmer day for
stack.top()! - Pop the index (
prevIndex). - Calculate days waiting:
i - prevIndex. - Update
answer[prevIndex].
- We found a warmer day for
- Push current index
ionto the stack.
Interactive Visualization
8 DAYS
ANSWER = 0
temperatures · answer
0
1
2
3
4
5
6
7
stack · waiting days
empty
Waiting
0
Answered
0
answer starts as zeros: a day with no warmer day after it keeps 0
Next
Day 0: 73°
Temps
Visualize how the monotonic stack keeps track of 'unresolved' cooler days.
Dry Run
Input: [73, 74, 75, 71, 69, 72]
| Day | Temp | Stack (Indices) | Action |
|---|---|---|---|
| 0 | 73 | [0] |
Push 0 |
| 1 | 74 | [] -> [1] |
74 > 73. Pop 0. ans[0] = 1-0 = 1. Push 1. |
| 2 | 75 | [] -> [2] |
75 > 74. Pop 1. ans[1] = 2-1 = 1. Push 2. |
| 3 | 71 | [2, 3] |
71 < 75. Push 3. |
| 4 | 69 | [2, 3, 4] |
69 < 71. Push 4. |
| 5 | 72 | [2] -> [2, 5] |
72 > 69 (Pop 4). 72 > 71 (Pop 3). Push 5. |
Edge Cases & Common Mistakes
- Decreasing Order:
[90, 80, 70]. Stack just grows. Output all 0s. - Stack Contents: Remember to store indices, not values.
Solution
Monotonic Stack O(N).
Daily Temperatures Code
1var dailyTemperatures = function(temperatures) {2 const n = temperatures.length;3 const answer = new Array(n).fill(0);4 const stack = []; // indices56 for (let i = 0; i < n; i++) {7 while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) {8 const idx = stack.pop();9 answer[idx] = i - idx;10 }11 stack.push(i);12 }13 return answer;14};
Daily Temperatures in Python, Java, C++, TypeScript and C
Daily Temperatures in Python
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
n = len(temperatures)
answer = [0] * n
stack = [] # stores indices
for i, t in enumerate(temperatures):
while stack and t > temperatures[stack[-1]]:
idx = stack.pop()
answer[idx] = i - idx
stack.append(i)
return answerDaily Temperatures in Java
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int n = temperatures.length;
int[] answer = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
int idx = stack.pop();
answer[idx] = i - idx;
}
stack.push(i);
}
return answer;
}
}Daily Temperatures in C++
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
vector<int> answer(n, 0);
stack<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && temperatures[i] > temperatures[st.top()]) {
int idx = st.top(); st.pop();
answer[idx] = i - idx;
}
st.push(i);
}
return answer;
}
};Daily Temperatures in TypeScript
function dailyTemperatures(temperatures: number[]): number[] {
const n = temperatures.length;
const answer: number[] = new Array(n).fill(0);
const stack: number[] = []; // indices
for (let i = 0; i < n; i++) {
while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) {
const idx = stack.pop()!;
answer[idx] = i - idx;
}
stack.push(i);
}
return answer;
};Daily Temperatures in C
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* dailyTemperatures(int* temperatures, int temperaturesSize, int* returnSize) {
int* answer = (int*)calloc(temperaturesSize, sizeof(int));
int* stack = (int*)malloc(temperaturesSize * sizeof(int));
int top = -1;
for (int i = 0; i < temperaturesSize; i++) {
while (top >= 0 && temperatures[i] > temperatures[stack[top]]) {
int idx = stack[top--];
answer[idx] = i - idx;
}
stack[++top] = i;
}
*returnSize = temperaturesSize;
free(stack);
return answer;
}More practice: DSA interview questions with answers · patterns cheat sheet
