Design a stack that also reports its smallest stored value at any moment.
You will process a stream of commands, one per line. Support three commands:
PUSH x — push the integer x onto the top of the stack. Produces no output.POP — remove the top element. Print OK if an element was removed, or EMPTY if the stack was already empty.MIN — print the smallest value currently on the stack, or EMPTY if the stack is empty.Output one line for every POP and MIN command, in the order the commands arrive.
Line 1: an integer q, the number of commands.
Each of the next q lines is one command: PUSH x, POP, or MIN.
For each POP command, print OK or EMPTY.
For each MIN command, print the current minimum, or EMPTY.
Print the results in command order, one per line.
Example 1
Input
5 PUSH 3 PUSH 1 MIN POP MIN
Expected
1 OK 3
Explanation
After `PUSH 3` and `PUSH 1` the stack is [3, 1]; `MIN` prints 1. `POP` removes the top (1) and prints `OK`. Now the stack is [3], so the final `MIN` prints 3.
Example 2
Input
3 MIN PUSH -4 MIN
Expected
EMPTY -4
Explanation
The first `MIN` runs on an empty stack and prints `EMPTY`. After `PUSH -4` the only element is -4, so the second `MIN` prints -4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →