DrawCode Algorithm Visualizer • Stack & Queue • Easy

Min Stack

Tags: Stack, Design, Auxiliary, O(1)

1. One-Liner

Maintain a parallel stack of current minimums so getMin reads the auxiliary top in O(1) alongside normal stack ops.


2. The Problem It Solves

Needed for sliding minima history, range queries on stack order, and design interview fundamentals.


3. The Core Idea

On push x, push min(x, aux.top or x); on pop, pop both stacks together—history stays synchronized.


4. How It Works (Step-by-Step)

StepAction
1Main stack s, min-stack m
2push: s.push(x); m.push(min(x,m.top or x))
3pop: both pop
4getMin: m.top

5. Dry Run Example

Push 5,1,3: mins [5,1,1]—pops restore prior min correctly.


6. Key Properties

PropertyDetail
Duplicate minsStore counts variant possible
SpaceO(n) worst for min stack
AlternativeStore deltas with one stack

7. Where It Is Used

Domain / SystemUse
IDE undoMin so far
FinanceRunning min trades
GamesScore stacks

8. Interview Tips

Mention one-stack delta trick if asked to save space; min heap is wrong for LIFO order.


9. Comparison with Other Algorithms

TechniqueNotes
HeapNot stack-ordered
Segment treeOverkill
RescanO(n) per query

10. Complexity

----
TimeO(1) each op
SpaceO(n)

Implementation Example (PYTHON)

class MinStack:
    def __init__(self):
        self.s = []
        self.m = []

    def push(self, x):
        self.s.append(x)
        self.m.append(x if not self.m else min(x, self.m[-1]))

    def pop(self): self.s.pop(); self.m.pop()
    def top(self): return self.s[-1]
    def getMin(self): return self.m[-1]

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer