Keep a second stack that stores the minimum value at each depth.
On push, also push min(val, currentMin) onto minStack. On pop, pop both stacks.
minStack mirrors the main stack, so its top always equals the minimum of all elements currently in the stack.
Keep a second stack that stores the minimum value at each depth.
On push, also push min(val, currentMin) onto minStack. On pop, pop both stacks.
minStack mirrors the main stack, so its top always equals the minimum of all elements currently in the stack.