Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element, all in O(1) time. Implement MinStack with push(val), pop(), top() returning the top element, and getMin() returning the smallest element currently in the stack.
Open official problem prompt ↗Support all four stack operations, including reading the current minimum, in constant time.
A stack of sticky notes where each note also records the smallest number written on any note from it downward, so the top note always tells you the overall minimum.
- Input
- ops = ["MinStack","push","push","push","getMin","pop","top","getMin"], args = [[],[-2],[0],[-3],[],[],[],[]]
- Output
- [null, null, null, null, -3, null, 0, -2]
- Why
- After pushing -2, 0, -3 the min is -3; popping -3 leaves top 0 and min -2.
-2^31 <= val <= 2^31 - 1pop, top, getMin are only called on a non-empty stackAt most 3 * 10^4 calls total across all methods