Min Stack

design · class

Description

Implement a stack that supports push, pop, top, and retrieving the
minimum element — all in O(1) time.

Methods:
    push(val) -> None
    pop()     -> None
    top()     -> int   (the value on top, without removing)
    getMin()  -> int   (the current minimum in the stack)

The trick: a single auxiliary stack of running minimums (or pairs of
(val, min_so_far)) keeps each operation O(1).

Constraints

- pop, top, getMin are only called on non-empty stacks
- Values are integers
- All four operations must be O(1)
solution.py
output
Run the cases to see results here.