- Min Stack
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
class MinStack {
Stack<Integer> s = new Stack<Integer>();
Stack<Integer> s2 = new Stack<Integer>();
public void push(int x) {
s.push(x);
if(s2.isEmpty()|| x<=s2.peek() ){
s2.push(x);
}
}
public void pop() {
if(s2.peek().equals(s.peek())){
s2.pop();
}
s.pop();
}
public int top() {
return s.peek();
}
public int getMin() {
return s2.peek();
}
}