The trick here is to use another stack which stores the minimum values of the original stack. The top of Min-stack always contains the minimum of stack elements so far inserted.
The Push operation is implemented as follows.
When an element is pushed on to the stack, we have to compare that with top of min-stack. If the new element is minimum, We have to push that element onto the min-stack too. We need not push that element on to min-stack if it is not lesser than top of min-stack.
The Pop operation is implemented as follows.
While popping the element from the original stack, we should also make sure that if minimum element is popped, it should also be removed from Min-stack.
The following table explains the logic.
Observe that top of min-stack always contains the minimum element.
Following is the Java code which implements the above approach.