Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
Created February 26, 2015 20:24
Show Gist options
  • Save dmnugent80/f12aa371867651e7facd to your computer and use it in GitHub Desktop.
Save dmnugent80/f12aa371867651e7facd to your computer and use it in GitHub Desktop.
Min Stack
class MinStack {
Stack<Integer> stk= new Stack<Integer>();
Stack<Integer> min= new Stack<Integer>();
public void push(int x) {
if (min.empty()){
min.push(x);
}
else{
if (x <= min.peek()){
min.push(x);
}
}
stk.push(x);
}
public void pop() {
if (stk.peek().equals( min.peek() )){
min.pop();
}
stk.pop();
}
public int top() {
return stk.peek();
}
public int getMin() {
return min.peek();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment