Sword refers to the offer interview question 59-2: the maximum value of the queue

The idea of ​​this question is similar to the previous one, my idea:

1 When pushing, the value is greater than the maximum value, the maximum value queue is cleared, and then this value is added to the end of the maximum queue, and the value is less than the maximum value, starting from the end of the queue, the data whose value is less than this value is deleted, and then this The value is added to the end of the maximum queue.

2 When pop, the value is equal to the maximum value, and the maximum value in the maximum value queue is deleted.

The idea mainly refers to the characteristics of the queue, the difference between the queue and the window:

The pop and push of the window occur at the same time, and two operations occur each time, making the length of the window unchanged; the queue pop and push occur separately.

 

Code:

The implementation involves the operation of Queue and Deque, which is not familiar. The code of the answer is very concise.

Deque<Integer> deque = new LinkedList();
Queue<Integer> queue = new LinkedList();
deque.size()
res = deque.peekLast();
deque.removeLast();
deque.addLast(value);
deque.peekFirst()
deque.removeFirst();
deque.addFirst(value);

 

My code

class MaxQueue {
    Deque<Integer> deque = new LinkedList();
    Queue<Integer> queue = new LinkedList();
    public MaxQueue() {

    }
    
    public int max_value() {
        int res = -1;
        if(deque.size() > 0){
            res = deque.peekLast();
        }
        return res;
    }
    
    public void push_back(int value) {
        if(queue.size() >0){
            queue.add(value);
            if(value > deque.peekLast()){
                while(deque.size()!=0){
                    deque.removeLast();
                }
                deque.addLast(value);
            }else{
                while(deque.size()!=0){
                    if(value > deque.peekFirst()){
                        deque.removeFirst();
                    }else{
                        break;
                    }
                }
                deque.addFirst(value);
            }
            
        }else{
            queue.add(value);
            deque.addLast(value);
        }
    }
    
    public int pop_front() {
        int quepop = -1;
        if(queue.size() >0){
            quepop = queue.remove();
            if(quepop == deque.peekLast()){
                while(deque.size()!=0){
                    if(deque.peekLast()==quepop){
                        deque.removeLast();
                    }else{
                        break;
                    }
                }
            }else{
                
            }
            
        }else{
            return -1;
        }
        return quepop;
    }
}

Answer code

class MaxQueue {
    Queue<Integer> q;
    Deque<Integer> d;

    public MaxQueue() {
        q = new LinkedList<Integer>();
        d = new LinkedList<Integer>();
    }
    
    public int max_value() {
        if (d.isEmpty()) {
            return -1;
        }
        return d.peekFirst();
    }
    
    public void push_back(int value) {
        while (!d.isEmpty() && d.peekLast() < value) {
            d.pollLast();
        }
        d.offerLast(value);
        q.offer(value);
    }
    
    public int pop_front() {
        if (q.isEmpty()) {
            return -1;
        }
        int ans = q.poll();
        if (ans == d.peekFirst()) {
            d.pollFirst();
        }
        return ans;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof/solution/mian-shi-ti-59-ii-dui-lie-de-zui-da-zhi-by-leetcod/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

Guess you like

Origin blog.csdn.net/qq_40473204/article/details/115345817