Back to Practice
#0252
Daily Temperatures
MediumDSA28 min20 XP
Problem
Given temperatures, return a list where answer[i] is the number of days until a warmer temperature. Use 0 if none exists.
Why This Matters
This is the standard monotonic stack pattern for resolving previous unanswered positions when a better future value arrives.
Function Signature
def daily_temperatures(temperatures):
Examples
Example 1
Inputtemperatures = [73, 74, 75, 71, 69, 72, 76, 73]
Output[1, 1, 4, 2, 1, 1, 0, 0]
Day 2 waits four days until 76. The last two days have no warmer future day.
Constraints
- Return the exact requested value.
- Handle empty or small inputs when the prompt allows them.
- Prefer the target DSA pattern over brute force when the input can grow.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Classic temperatures
Input[73,74,75,71,69,72,76,73]
Expected[1,1,4,2,1,1,0,0]
Each warmer day resolves colder days on the stack.
Always decreasing
Input[80, 70, 60]
Expected[0, 0, 0]
No future day is warmer.
Hidden Test Categories
equal temperaturessingle daystrictly increasing temperatures