public class MinStack { Stack s1 = new Stack(); Stack s2 = new Stack(); public void push(int x) { s1.push(x); if (x <= min) { s2.push(x); min = x; } } public void pop() { int tmp = s1.pop(); if (tmp == min) { s2.pop(); if (!s2.isEmpty()) min = s2.peek(); else min = Integer.MAX_VALUE; } } public int top() { return s1.peek(); } }//end class MinStack