Skip to content

Instantly share code, notes, and snippets.

@kelly-us
Created April 27, 2015 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelly-us/4151fd5c56e2cb9090cb to your computer and use it in GitHub Desktop.
Save kelly-us/4151fd5c56e2cb9090cb to your computer and use it in GitHub Desktop.
public class StackWithMin extends Stack<Integer>{
Stack<Integer> s;
public StackWithMin(){
s = new Stack<Integer>();
}
public void push(int val){
if(val <= min()){
s.push(val);
}
super.push(val);
}
public Integer pop(){
int ret = super.pop();
if(ret == min()){
s.pop();
}
return ret;
}
public int min(){
if(s.isEmpty()){
return Integer.MAX_VALUE;
}
return s.peek();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment