Min Stack

Medium Solved

Description

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Implement the MinStack class:

  • push(val) - pushes the element onto the stack
  • pop() - removes the top element
  • top() - returns the top element
  • getMin() - returns the minimum element

Example:

Input:
push(-2)
push(0)
push(-3)
getMin()
pop()
top()
getMin()

Output:
-3
0
-2

Note:

For the runner, simulate operations inside code and print outputs line by line.

Your Submissions

No submissions yet.

Discuss

Discuss auxiliary stack approach vs encoding values.

Test Cases