Min Stack
MediumPython 3Stacks
Design a stack that supports each of these operations in O(1) time: - push(value): push `value` onto the stack. - pop(): remove the top element. - top(): return the top element without removing it. - get_min(): return the smallest element currently on the stack.
Constraints: - values are integers (possibly negative, duplicates allowed) - pop, top and get_min are only called on a non-empty stack
Examples: s = MinStack() s.push(-2); s.push(0); s.push(-3) s.get_min() -> -3 s.pop() s.top() -> 0 s.get_min() -> -2
Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.
Loading the editor…