```
List<Integer> stack = new ArrayList<Integer>();
Integer min = null;
public void push(int x) {
stack.add(x);
if(min !=null){
if(x < min){
min = x;
}
}
}
public void pop() {
int temp = stack.get(stack.size()-1);
if(min !=null && temp == min){
min = null;
}
stack.remove(stack.size()-1);
}
public int top() {
return stack.get(stack.size()-1);
}
public int getMin() {
if(min != null){
return min;
}
min = stack.get(0);
for(int i=1;i<stack.size();i++){
int temp = stack.get(i);
if(temp < min){
min = temp;
}
}
return min;
}
```