Skip to main content
  1. LeetCode/

LeetCode 739: Daily Temperatures

·2 mins· ·
LeetCode Daily Medium Stack Monotonic-Stack Array Temperature Waiting-Time Algorithm Data-Structure
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: Medium
First Attempt: 2025-08-10
Total Time: 00:00.00
Category: Stack, Monotonic Stack, Array
Related Topics: Temperature Monitoring, Waiting Time Calculation, Stack Operations

Intuition
#

The key insight is that for each temperature, we need to find the next warmer temperature. This can be efficiently solved using a monotonic stack approach, where we maintain a stack of temperatures in decreasing order. When we encounter a warmer temperature, we can update the waiting days for all previous colder temperatures.

Approach
#

Using Monotonic Stack for Next Warmer Temperature
#

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        n = len(temperatures)
        res = [0] * n
        stack = []

        for i in range(n):
            while stack and temperatures[stack[-1]] < temperatures[i]:
                idx = stack.pop()
                res[idx] = i - idx
            
            stack.append(i)

        return res

Time Complexity: O(n) where n is the length of temperatures array
Space Complexity: O(n) for the stack in worst case

Key Insights
#

  1. Monotonic Stack Pattern: We use a stack to maintain temperatures in decreasing order
  2. Next Warmer Temperature: When we find a warmer temperature, we can calculate waiting days for all colder temperatures
  3. Index Tracking: We store indices in the stack to calculate the distance between temperatures
  4. Efficient Updates: Each temperature can only be pushed and popped once, leading to O(n) time complexity

Findings
#

  • The monotonic stack approach is perfect for problems involving finding the next greater/smaller element
  • This problem demonstrates how to use stack indices to calculate distances efficiently
  • The time complexity is optimal at O(n) since each element can only be pushed and popped once
  • The stack naturally maintains the decreasing order, making it easy to find the next warmer temperature

Encountered Problems
#

Related

LeetCode 901: Online Stock Span
·2 mins
LeetCode Daily Medium Stack Monotonic-Stack Design Data-Structure Algorithm
Solving the Online Stock Span problem using monotonic stack approach
LeetCode 150: Evaluate Reverse Polish Notation
·2 mins
LeetCode Daily Medium Stack Expression-Evaluation Reverse-Polish-Notation Postfix-Notation Mathematics Algorithm Data-Structure
Solving the Reverse Polish Notation evaluation problem using stack-based approach
LeetCode 84: Largest Rectangle in Histogram
·3 mins
LeetCode Daily Hard Stack Monotonic-Stack Array Histogram Geometry Algorithm Dynamic-Programming
Solving the Largest Rectangle in Histogram problem using monotonic stack approach
LeetCode 503: Next Greater Element II (Monotonic Stack)
·2 mins
LeetCode Daily Medium Monotonic-Stack Stack Array Circular-Array
Monotonic stack on a circular array
LeetCode 20: Valid Parentheses
·2 mins
LeetCode Daily Easy Stack String Parentheses Validation Algorithm Data-Structure
Solving the Valid Parentheses problem using stack-based approach
LeetCode 853: Car Fleet
·2 mins
LeetCode Daily Medium Stack Sorting Simulation Greedy Array
Solving the Car Fleet problem using stack-based approach