Solution to Min In Stack from Jobdu

10 Aug

Question (in Chinese):

Question Name: Min In Stack

Question Description: implement a customized stack. It should provide a function min() to return the minimum value in the stack in O(1) time.

Input: the input might contain multiple test cases. Inside each test case, the first line includes one intergers N (1 <= N <= 1000000), saying the number of following instructions. The following N lines contain the instructions. Each instruction might be either:
s INTEGER: where s is the “push” command, and the INTEGER is an number as the paramter; OR
o: where o is the “pop” command.

Output: NULL if the stack is empty. Otherwise, print the minimum value after each instruction.

The solution is:

Leave a Reply

Your email address will not be published. Required fields are marked *