← 返回 linkedin 的题目列表Maximum Stack
类型:online_judge
Design a max stack that supports the following operations.
push(x) —— Push element x onto the stack.
pop() —— Remove the element on the top of the stack and return it.
top() —— Get the element on the top.
peekMax() —— Retrieve the maximum element in the stack.
popMax() —— Remove and return the maximum element in the stack. If there are multiple maximum elements, only remove the one closest to the top.
The push, top, and peekMax operations should all run in O(1), and popMax in O(log n) time complexity.
Example
Input
push 4
push 7
push 7
popMax
peekMax
pop