#155

Min Stack

Medium Stack of (value, min) pairs

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Design a stack that supports push, pop, top and getMin, all in O(1).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: a stack with getMin in O(1).

Why this works: store with every value the minimum at that moment; popping restores the old minimum automatically.

Example: push -2, 0, -3

[(-2,-2), (0,-2), (-3,-3)] → min -3pop → min -2

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Keeping a single min variable — after popping the minimum you don't know the next one.

Solve on LeetCode ↗
Complexity
  • Time: O(1) per operation.
  • Space: O(n).
Topic: Stack & Queue
🎬 Episodes 46–52: video coming soon