#C10556. Special Stack with O(1) Minimum Operation
Special Stack with O(1) Minimum Operation
Special Stack with O(1) Minimum Operation
This problem requires you to implement a special stack that supports the standard operations push
, pop
, top
and an additional operation min
which retrieves the minimum element in the stack. All operations must run in O(1) time complexity.
The stack should behave as follows:
- push x: Push the integer
x
onto the stack. - pop: Remove the top element from the stack and print its value. If the stack is empty, print
None
. - top: Print the top element of the stack without removing it. If the stack is empty, print
None
. - min: Print the minimum element currently in the stack. If the stack is empty, print
None
.
For example, consider the following sequence of operations:
push 3 push 5 min push 2 push 1 min pop min pop top
The expected output is:
3 1 2 5
Implement the data structure and simulate the operations as described. The input will be read from standard input and output should be printed to standard output.
inputFormat
The first line contains an integer n representing the number of commands. The next n lines each contain a command. A command is one of the following:
push x
: wherex
is an integerpop
top
min
There is no output for the push
command.
outputFormat
For each pop
, top
, or min
command, output the result on a separate line. If the stack is empty when a pop
, top
, or min
command is executed, output None
.
10
push 3
push 5
min
push 2
push 1
min
pop
min
pop
top
3
1
2
5
</p>