716. Max Stack (follow up questions for min stack)

Design a max stack that supports push, pop, top, peekMax and popMax.

push(x) -- Push element x onto stack.
pop() -- Remove the element on top of the stack and return it.
top() -- Get the element on the top.
peekMax() -- Retrieve the maximum element in the stack.
popMax() -- Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.
Example 1:
MaxStack stack = new MaxStack();
stack.push(5); 
stack.push(1);
stack.push(5);
stack.top(); -> 5
stack.popMax(); -> 5
stack.top(); -> 1
stack.peekMax(); -> 5
stack.pop(); -> 1
stack.top(); -> 5
Note:
-1e7 <= x <= 1e7
Number of operations won't exceed 10000.
The last four operations won't be called when stack is empty.

1. Solution: add popmax this func, you can pop out max at any time , so max stack to track the max elements  does not work as minstack(leetcode 155) did

add one more stack for helping

1. pop out the ele in the origin stack until find the max

2. push all into temp stack

3. use push(self built func) to push all the ele from temp stack into original stack

class MaxStack {
    //becasuse of popMAx, two stacks is not enough
    List<Integer> s1 ;//store ele
    List<Integer> s2 ;//store min ele
    /** initialize your data structure here. */
    public MaxStack() {
        s1 = new ArrayList<Integer>();
        s2 = new ArrayList<Integer>();
    }
    
    public void push(int x) {
        s1.add(x);
        if(s2.isEmpty() || s2.get(s2.size()-1) <= x) s2.add(x);
    }
    
    public int pop() {
        if(s1.isEmpty()) return 0;
        int ele = s1.remove(s1.size()-1);
        if(!s2.isEmpty() && ele == s2.get(s2.size()-1)){
            s2.remove(s2.size()-1);
        }
        return ele;
    }
    
    public int top() {
        if(!s1.isEmpty())
        return s1.get(s1.size()-1);
        return 0;
    }
    
    public int peekMax() {
        if(!s2.isEmpty())
         return s2.get(s2.size()-1);
        return 0;
    }
    
    //handful problem
    public int popMax() {
        //pop max
        if(!s1.isEmpty() && !s2.isEmpty()){
            int ele = s2.remove(s2.size() - 1);
            List<Integer> s3 = new ArrayList<>();
            while(ele != s1.get(s1.size() - 1)){
                int temp = s1.remove(s1.size() - 1);
                s3.add(temp);
            }
            s1.remove(s1.size() - 1);
            while(!s3.isEmpty()){
                push(s3.remove(s3.size()-1));
            }
            return ele;
        }
        return 0;
    }
}

/**
 * Your MaxStack object will be instantiated and called as such:
 * MaxStack obj = new MaxStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.peekMax();
 * int param_5 = obj.popMax();
 */

2. solution 2: https://leetcode.com/problems/max-stack/discuss/153748/Java-LinkedList-and-PriorityQueue  (using linkedlist and priorityQueue)

Leave a comment u have a question!

 

posted @ 2018-08-09 13:28  wz30  阅读(335)  评论(0编辑  收藏  举报