-
-
Save kelly-us/4151fd5c56e2cb9090cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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