Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MinStack {
- Stack<Integer> stack = new Stack();
- Stack<Integer> min = new Stack();
- public MinStack() {
- }
- public void push(int val) {
- stack.push(val);
- if(min.isEmpty() || min.peek()>=val){
- min.push(val);
- }
- }
- public void pop() {
- if(stack.peek().equals(min.peek())){
- min.pop();
- }
- stack.pop();
- }
- public int top() {
- return stack.peek();
- }
- public int getMin() {
- return min.peek();
- }
- }
- /**
- * Your MinStack object will be instantiated and called as such:
- * MinStack obj = new MinStack();
- * obj.push(val);
- * obj.pop();
- * int param_3 = obj.top();
- * int param_4 = obj.getMin();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement