Created
February 26, 2015 20:24
-
-
Save dmnugent80/f12aa371867651e7facd to your computer and use it in GitHub Desktop.
Min Stack
This file contains hidden or 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
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