Problem Statement
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Approach
We maintain two stacks: a main stack for the actual values and a "min stack" that tracks the minimum value at each level. When pushing a value, we push it to the main stack and push the new minimum (min of current value and current top of min stack) to the min stack.
Time & Space Complexity
Time complexity is O(1) for all operations (push, pop, top, getMin). Space complexity is O(n) to store the auxiliary min stack.
