0
点赞
收藏
分享

微信扫一扫

【LeetCode 剑指 Offer 59 - II. 队列的最大值(中等)】

静守幸福 2022-05-04 阅读 57

题目:

请定义一个队列并实现函数 max_value 得到队列里的最大值,要求函数max_value、push_back 和 pop_front 的均摊时间复杂度都是O(1)。

若队列为空,pop_front 和 max_value 需要返回 -1

在这里插入图片描述

解题过程:

思路源自LeetCode用户:mata川
其实本质上是一个求滑动窗口最大值的问题。这个队列可以看成是一个滑动窗口,入队就是将窗口的右边界右移,出队就是将窗口的左边界右移。

既然是求解滑动窗口的最大值问题,那就变成了模板题,直接套单调队列模板即可。

class MaxQueue {
    private Deque<Integer> queue;
    private Deque<Integer> help;

    public MaxQueue() {
        queue = new ArrayDeque<>();
        help = new ArrayDeque<>();
    }
    
    public int max_value() {
        return queue.isEmpty() ? -1 : help.peek();
    }
    
    public void push_back(int value) {
        queue.offer(value);
        while(!help.isEmpty() && value > help.peekLast()){
            help.pollLast();
        }
        help.offer(value);
    }
    
    public int pop_front() {
        if(queue.isEmpty()){
            return -1;
        }
        int val = queue.pop();
        if(help.peek() == val){
            help.pop();
        }
        return val;
    }
}

/**
 * Your MaxQueue object will be instantiated and called as such:
 * MaxQueue obj = new MaxQueue();
 * int param_1 = obj.max_value();
 * obj.push_back(value);
 * int param_3 = obj.pop_front();
 */

执行结果:

在这里插入图片描述

举报

相关推荐

0 条评论