#C11242. MinStack Implementation
MinStack Implementation
MinStack Implementation
Implement a stack that supports the following operations in O(1) time complexity:
push x
— Push integer x onto the stack.pop
— Remove the top element of the stack.top
— Return the top element of the stack. If the stack is empty, outputNone
.getMin
— Return the minimum element in the stack. If the stack is empty, outputNone
.
You will be given a sequence of operations, and you must process them in order. For each top
and getMin
operation, output the result on a new line.
inputFormat
The input is given via standard input (stdin) and has the following format:
- The first line contains a single integer n (n ≥ 1), representing the number of operations.
- Each of the next n lines contains one operation, which can be one of the following:
push x
— where x is an integer to push onto the stack.pop
— remove the top element of the stack.top
— output the top element of the stack; outputNone
if the stack is empty.getMin
— output the minimum element in the stack; outputNone
if the stack is empty.
outputFormat
For each top
and getMin
operation, output the corresponding result on a new line using standard output (stdout). For operations that do not require an output (push
and pop
), do not print anything. If an output is expected but the stack is empty, print None
(without quotes).
7
push -2
push 0
push -3
getMin
pop
top
getMin
-3
0
-2
</p>