Min Stack

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Min Stack

Approach

Use one stack for values and a second stack to track current minimum values. Push into the min stack whenever the new value is smaller than or equal to the current minimum. When popping, also pop from min stack if the removed value equals the current minimum.

Complexity Analysis

Time
O(1)
Space
O(n)

Pattern

Auxiliary Stack

Why It Works

The min stack stores the minimum value for each state where minimum changes. This keeps current minimum available at the top, so getMin is constant time.

Updated Feb 2026