Design a stack that supports push, pop, peek, and retrieving the minimum element in constant time.
Your MinStack class should have the following methods:
push(x): Pushes element x onto the stack.pop(): Removes the element on peek of the stack.peek(): Gets the peek element.getMin(): Retrieves the minimum element in the stack.m = MinStack() m.push(2) m.push(0) m.push(1) m.get_min() m.pop() m.peek() m.get_min()
[0, 1, 0, 0]
m = MinStack() m.push(2) m.push(1) m.peek() m.get_min() m.pop() m.peek() m.get_min() m.push(5) m.get_min()
[1, 1, 1, 2, 2, 2]
m = MinStack() m.push(2) m.push(0) m.push(1) m.get_min() m.pop() m.peek() m.get_min()
[0, 1, 0, 0]