Skip to main content
  1. LeetCode/

LeetCode 155: Min Stack

·2 mins· ·
LeetCode Daily Medium Stack Constant-Time Data-Structures Design Tuple
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 Data-Structures Design Design-Patterns Stock
Solving the Online Stock Span problem using monotonic stack approach
LeetCode 150: Evaluate Reverse Polish Notation
·2 mins
LeetCode Daily Medium Stack Data-Structures Expression-Evaluation Math Postfix-Notation Reverse-Polish-Notation
Solving the Reverse Polish Notation evaluation problem using stack-based approach
LeetCode 739: Daily Temperatures
·2 mins
LeetCode Daily Medium Array Stack Monotonic-Stack Data-Structures Temperature Waiting-Time
Solving the Daily Temperatures problem using monotonic stack approach
LeetCode 20: Valid Parentheses
·2 mins
LeetCode Daily Easy String Stack Data-Structures Parentheses Validation
Solving the Valid Parentheses problem using stack-based approach
LeetCode 853: Car Fleet
·2 mins
LeetCode Daily Medium Array Stack Greedy Simulation Sorting
Solving the Car Fleet problem using stack-based approach
LeetCode 503: Next Greater Element II (Monotonic Stack)
·2 mins
LeetCode Daily Medium Array Stack Monotonic-Stack Circular-Array
Monotonic stack on a circular array