Min Stack
Mediumjavapythonccppjavascript
Design a stack that supports push, pop, top and getMin (retrieving the minimum value), all in constant time.
Input Format
The first line contains an integer Q. Each of the next Q lines is one of: push X, pop, top, getMin.
Output Format
For each pop print the removed element (or -1 if empty). For each top print the top element (or -1). For each getMin print the current minimum (or -1 if empty). One value per line.
Example 1
Input
4 push 5 push 2 push 7 getMin
Output
2
Explanation: Among 5, 2 and 7 the minimum is 2.
- 1 <= Q <= 100000
- -1000000000 <= X <= 1000000000