Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Examples
Input: [["MinStack","push","push","push","getMin","pop","top","getMin"],[[],[-2],[0],[-3],[],[],[],[]]]
Output: [null,null,null,null,-3,null,0,-2]
Input: [["MinStack","push","getMin"],[[],[1],[]]]
Output: [null,null,1]
Hints
Use a single stack where each element is a tuple containing the value and the current minimum value up to that point.
Alternatively keep a second stack of running minima: on push, compare the new value with the current minimum (top of the min stack) and record the smaller one.
On pop, remove the top from both structures together; the entry below still carries the minimum of everything that remains.
Related Problems
Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.