Skip to main content
  1. LeetCode/

LeetCode 155: Min Stack

·2 mins· ·
LeetCode Daily Medium Stack Design Data-Structure Tuple Constant-Time
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

Intuition
#

This is a stack problem where we need to track the minimum element at any given time. To achieve O(1) time complexity for the getMin() operation, we can store each element along with the current minimum value in a tuple. This way, every element in the stack knows what the minimum was when it was pushed.

Approach
#

The solution uses a tuple-based approach where each stack element contains:

  1. Value: The actual value being pushed
  2. Current Minimum: The minimum value in the stack at the time of pushing

This design allows us to:

  • Push: O(1) - Calculate new minimum and store with value
  • Pop: O(1) - Remove top element
  • Top: O(1) - Return top value
  • GetMin: O(1) - Return current minimum from top element
class MinStack:

    def __init__(self):
        self.stack: list[tuple] = []

    def push(self, val: int) -> None:
        min_latest = self.stack[-1][1] if self.stack else float("inf")
        min_latest = min(val, min_latest)
        self.stack.append((val, min_latest))

    def pop(self) -> None:
        val, _ = self.stack.pop()
        return val

    def top(self) -> int:
        val, _ = self.stack[-1] if self.stack else (None, None)
        return val

    def getMin(self) -> int:
        _, min_num = self.stack[-1] if self.stack else (None, None)
        return min_num

Time Complexity: O(1) for all operations Space Complexity: O(n) where n is the number of elements pushed

Findings
#

  • Tuple-based design: Using tuples to store value and minimum together is elegant and efficient
  • Constant time operations: All stack operations maintain O(1) time complexity
  • Space trade-off: We use extra space to store minimum values, but gain constant time access
  • Design pattern: This demonstrates how to design data structures with specific performance guarantees

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 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 739: Daily Temperatures
·2 mins
LeetCode Daily Medium Stack Monotonic-Stack Array Temperature Waiting-Time Algorithm Data-Structure
Solving the Daily Temperatures problem using monotonic stack 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 853: Car Fleet
·2 mins
LeetCode Daily Medium Stack Sorting Simulation Greedy Array
Solving the Car Fleet problem using stack-based approach