Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Stack High frequency Solution Available Practice Mode

Daily Temperatures LeetCode Solution

Tags: #Array #Stack #Monotonic Stack

Approach Summary

Monotonic decreasing stack of indices. When current temp > stack top temp, pop and record the wait time.

Practice Mode

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Stack Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Stack pattern.

Framework: Stack • Interview Prep: Tested at FAANG • Explore all Stack problems & invariants →

How to Recognize This Pattern

  • Next greater element with distance
  • Days until warmer

Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(n)

Tags

Array Stack Monotonic Stack

Frequently Asked Questions

What is the optimal time and space complexity for Daily Temperatures?
The optimal solution for Daily Temperatures achieves O(n) runtime complexity and requires O(n) auxiliary space. Each index enters and leaves the stack exactly once — O(n) time. The stack holds up to n indices — O(n) space.
How do I identify that Daily Temperatures requires the Stack pattern?
In technical interviews, look for key problem signals: Next greater element with distance; Days until warmer. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Stack strategy.
What critical edge cases must be handled in Daily Temperatures?
Essential edge cases to verify include: Monotonically decreasing temperatures — all answers are 0; Monotonically increasing temperatures — every answer is 1; The hottest day — always 0; Equal temperatures — the strict comparison means equal days do not resolve each other. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Daily Temperatures?
Daily Temperatures is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Google, Facebook.
Buy me a coffee
Daily Practice
0/0