#K55852. Maintain Minimum on a Stack
Maintain Minimum on a Stack
Maintain Minimum on a Stack
You are given a sequence of operations to be performed on a custom stack. The operations are of two types: "add x", which pushes the integer x onto the stack, and "remove", which pops the top element from the stack.
After every "remove" operation, output the minimum element currently in the stack. If the stack is empty after a removal, output \(\text{None}\). To achieve efficient minimum retrieval, you are required to implement a MinStack
data structure that supports these operations in O(1) time.
Note: The input will first contain an integer denoting the number of operations, followed by that many lines each containing an operation. The output is produced only after each "remove" operation.
Good luck!
inputFormat
The first line of input contains an integer \(N\), the number of operations. The following \(N\) lines each contain an operation in one of the following formats:
add x
— Push integer \(x\) onto the stack.remove
— Remove the top element from the stack and then report the current minimum value.
outputFormat
For each "remove" operation, print the minimum element in the stack on a new line. If the stack is empty, print None
.
6
add 3
add 5
add 2
remove
add 1
remove
3
3
</p>