Daily Temperatures
Daily Temperatures is a medium monotonic stack problem solved with the monotonic stack pattern.
The best approach, optimal (monotonic stack of indices), runs in O(n) time and O(n) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force up.
Problem
Given daily temperatures, return an array where answer[i] is the number of days until a warmer temperature after day i. If no warmer day comes, use 0.
Examples
Example 1
- Input
temperatures = [30, 32, 31, 29, 35, 30]- Output
[1, 3, 2, 1, 0, 0]- Why
- Day 1 (32) waits 3 days for 35.
Example 2
- Input
temperatures = [50, 40, 30]- Output
[0, 0, 0]
Constraints
1 <= temperatures.length <= 10^530 <= temperatures[i] <= 100
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force | O(n²) | O(1) |
| Optimal (monotonic stack of indices) | O(n) | O(n) |
1Brute force
O(n²)O(1)For each day, scan forward to the first warmer day.
- For each i, find the smallest j > i with t[j] > t[i]; answer j - i.
class Solution {
public int[] dailyTemperatures(int[] t) {
int n = t.length;
int[] out = new int[n];
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (t[j] > t[i]) { out[i] = j - i; break; }
return out;
}
}class Solution:
def dailyTemperatures(self, t: List[int]) -> List[int]:
n = len(t)
out = [0] * n
for i in range(n):
for j in range(i + 1, n):
if t[j] > t[i]:
out[i] = j - i
break
return outclass Solution {
public:
vector<int> dailyTemperatures(vector<int>& t) {
int n = t.size();
vector<int> out(n);
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (t[j] > t[i]) { out[i] = j - i; break; }
return out;
}
};function dailyTemperatures(t) {
const n = t.length;
const out = new Array(n).fill(0);
for (let i = 0; i < n; i++)
for (let j = i + 1; j < n; j++)
if (t[j] > t[i]) { out[i] = j - i; break; }
return out;
}2Optimal (monotonic stack of indices)
O(n)O(n)Keep a stack of indices of days still waiting for a warmer day, with decreasing temperatures. A warmer day pops every cooler day on top and fills in the distance.
- For each i: while t[stack top] < t[i], j = pop(); out[j] = i - j.
- Push i.
class Solution {
public int[] dailyTemperatures(int[] t) {
int[] out = new int[t.length];
Deque<Integer> st = new ArrayDeque<>();
for (int i = 0; i < t.length; i++) {
while (!st.isEmpty() && t[st.peek()] < t[i]) {
int j = st.pop();
out[j] = i - j;
}
st.push(i);
}
return out;
}
}class Solution:
def dailyTemperatures(self, t: List[int]) -> List[int]:
out = [0] * len(t)
st = []
for i in range(len(t)):
while st and t[st[-1]] < t[i]:
j = st.pop()
out[j] = i - j
st.append(i)
return outclass Solution {
public:
vector<int> dailyTemperatures(vector<int>& t) {
vector<int> out(t.size());
vector<int> st;
for (int i = 0; i < (int)t.size(); i++) {
while (!st.empty() && t[st.back()] < t[i]) {
int j = st.back();
st.pop_back();
out[j] = i - j;
}
st.push_back(i);
}
return out;
}
};function dailyTemperatures(t) {
const out = new Array(t.length).fill(0);
const st = [];
for (let i = 0; i < t.length; i++) {
while (st.length > 0 && t[st[st.length - 1]] < t[i]) {
const j = st.pop();
out[j] = i - j;
}
st.push(i);
}
return out;
}Edge cases to test
- Equal temperatures are not warmer
- Strictly decreasing input
Hints
Hint 1
Store indices, not temperatures, on the stack so you can compute the distance.
FAQ
What is the best time complexity for Daily Temperatures?
Optimal (monotonic stack of indices) runs in O(n) time and O(n) extra space.
Which pattern does Daily Temperatures use?
It is a monotonic stack problem that uses the monotonic stack pattern. Other problems with the same pattern: Next Greater Element I, Next Greater Element II, Largest Rectangle in Histogram.
Is there a brute force solution for Daily Temperatures?
Yes. Brute force takes O(n²) time and O(1) space. For each day, scan forward to the first warmer day.
Which edge cases should I test for Daily Temperatures?
Equal temperatures are not warmer; Strictly decreasing input.