package getmin;
import java.util.Stack;
import javax.management.RuntimeErrorException;
public class MyStack {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack(){
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int newNum){
if(this.stackMin.isEmpty()){
this.stackMin.push(newNum);
}else if(newNum <= this.getmin()){
this.stackMin.push(newNum);
}else{
this.stackData.push(newNum);
}
}
public int pop(){
if(this.stackData.isEmpty()){
throw new RuntimeException("your stack is empty");
}
int value = this.stackData.pop();
if(value == this.getmin()){
this.stackMin.pop();
}
return value;
}
public int getmin(){
if(this.stackMin.isEmpty()){
throw new RuntimeException("your stack is empty");
}
return this.stackMin.peek();
}
public static void main(String args[])
{
MyStack sta = new MyStack();
sta.push(3);
sta.push(4);
sta.push(2);
sta.push(1);
sta.pop();
int min = sta.getmin();
System.out.println(min);
}
}