import java.util.Stack;

/**
* 实现一个栈,在基本功能的基础上,可以返回栈中最小值
*/
public class GetMinStack {

public static class MyStack {

public Stack<Integer> stackData;

public Stack<Integer> stackMin;

public MyStack() {
this.stackData = new Stack<>();
this.stackMin = new Stack<>();
}

public void push(Integer value) {
if (stackMin.isEmpty()) {
stackMin.push(value);
} else if (value <= getMin()) {
stackMin.push(value);
} else {
stackMin.push(getMin());
}
stackData.push(value);
}

public Integer pop() {
if (stackData.isEmpty()) {
System.out.println("the stack is empty");
return null;
}
stackMin.pop();
return stackData.pop();
}

public Integer getMin() {
if (stackMin.isEmpty()) {
System.out.println("the stack is empty");
return null;
}
return stackMin.peek();
}

}

public static class MyStack2 {

public Stack<Integer> stackData;

public Stack<Integer> stackMin;

public MyStack2() {
this.stackData = new Stack<>();
this.stackMin = new Stack<>();
}

public void push(Integer value) {
if (stackMin.isEmpty()) {
stackMin.push(value);
} else if (value <= getMin()) {
stackMin.push(value);
}
stackData.push(value);
}

public Integer pop() {
if (stackData.isEmpty()) {
System.out.println("the stack is empty");
return null;
}
if (stackData.peek() == getMin()) {
stackMin.pop();
}
return stackData.pop();
}

public Integer getMin() {
if (stackMin.isEmpty()) {
System.out.println("the stack is empty");
return null;
}
return stackMin.peek();
}

}

}

/* 如有错误,欢迎批评指正 */