Max-stack

medium
Design a max stack data structure that supports the stack operations and supports finding the stack's maximum element.

Implement the MaxStack class:
1. MaxStack() Initializes the stack object.
2. void push(int x) Pushes element x onto the stack.
3. int pop() Removes the element on top of the stack and returns it.
4. int top() Gets the element on the top of the stack without removing it.
5. int peekMax() Retrieves the maximum element in the stack without removing it.
6. int popMax() Retrieves the maximum element in the stack and removes it. If there is more than one maximum element, only remove the top-most one.

Input Format

Input is managed for you

Output Format

Output is managed for you

Constraints

1. There will be at least 1 element when pop, peek, popmax, peekmax is called.

Notice

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

Example

Input
push 5
push 1
push 5
top
popMax
top
peekMax
pop
top
Output
5
5
1
5
1
5
Previous
Lexicographically Smallest Subsequence
Next
Check If Word Is Valid After Insertion

Related Questions