← 返回 amazon 的题目列表Optimize Min Stack
类型:online_judge
Design a stack that supports push(x), pop(), top(), and getMin() all in O(1) time complexity.
push(x) — Push element x onto stack.
pop() — Removes the element on top of the stack.
top() — Get the top element.
getMin() — Retrieve the minimum element in the stack.
Constraints:
All operations must have O(1) time complexity.
Aside from the data storage for n elements, no additional data structures such as tuple/pair should be used.
Test Case:
Input: push(-2), push(0), push(-3), getMin(), pop(), top(), getMin()
Output: -3, 0, -2
Explanation: After performing push(-2), push(0), push(-3) operations, the stack contains [-2, 0, -3]. getMin() returns -3. After pop(), the stack contains [-2, 0], top() returns 0. getMin() returns -2.
Example
Input
push -2
push 0
push -3
getMin
pop
top
getMin