#C11242. MinStack Implementation

    ID: 40537 Type: Default 1000ms 256MiB

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, output None.
  • getMin — Return the minimum element in the stack. If the stack is empty, output None.

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:

  1. The first line contains a single integer n (n ≥ 1), representing the number of operations.
  2. 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; output None if the stack is empty.
    • getMin — output the minimum element in the stack; output None 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).

## sample
7
push -2
push 0
push -3
getMin
pop
top
getMin
-3

0 -2

</p>