ArrayList<Integer> aList; ArrayList<Integer> minList; public MinStack() { this.aList = new ArrayList<>(); this.minList = new ArrayList<>(); }
public void push(int number) { if (aList.size() == 0) { aList.add(number); minList.add(number); } else { aList.add(number); if (number <= minList.get(minList.size()-1)) { minList.add(number); } } }
public int pop() { int popNum = aList.remove(aList.size()-1); if(popNum == minList.get(minList.size()-1)) { minList.remove(minList.size()-1); } return popNum; } public int min() { return minList.get(minList.size()-1); }