力扣labuladong——一刷day87

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


现在需要一种新的队列结构,既能够维护队列元素「先进先出」的时间顺序,又能够正确维护队列中所有元素的最值,这就是「单调队列」结构。

一、力扣503. 下一个更大元素 II

class Solution {
   
    public int[] nextGreaterElements(int[] nums) {
   
        int n = nums.length;
        int[] arr = new int[2*n];
        int[] res = new int[n];
        for(int i = 0; i < 2*n; i ++){
   
            if(i < n){
   
                arr[i] = nums[i];
            }else{
   
                arr[i] = nums[i-n];
            }
        }
        Deque<Integer> deq = new LinkedList<>();
        for(int i = 2*n-1; i >= 0; i --){
   
            while(!deq.isEmpty() && deq.peekLast() <= arr[i]){
   
                deq.pollLast();
            }
            if(i < n){
   
                res[i] = deq.isEmpty() ? -1 : deq.peekLast();
            }
            deq.offerLast(arr[i]);
        }
        return res;
    }
}

二、力扣239. 滑动窗口最大值

class Solution {
   
    public int[] maxSlidingWindow(int[] nums, int k) {
   
        MonotonicDeque mq = new MonotonicDeque();
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < nums.length; i ++){
   
            if(i < k-1){
   
                mq.push(nums[i]);
            }else{
   
                mq.push(nums[i]);
                list.add(mq.getMax());
                mq.remove(nums[i-k+1]);
            }
        }
        int[] res = new int[list.size()];
        for(int i = 0; i < list.size() ; i ++){
   
            res[i] = list.get(i);
        }
        return res;
    }
    class MonotonicDeque{
   
        LinkedList<Integer> deq;
        public MonotonicDeque(){
   
            this.deq = new LinkedList<Integer>();
        }
        public void push(int x){
   
            while(!deq.isEmpty() && deq.getLast() < x){
   
                deq.pollLast();
            }
            deq.addLast(x);
        }
        public int getMax(){
   
            return deq.getFirst();
        }
        public void remove(int x){
   
            if(!deq.isEmpty() && deq.getFirst() == x){
   
                deq.pollFirst();
            }
        }
    }
}

相关推荐

  1. labuladong——day87

    2024-01-08 15:38:03       38 阅读
  2. labuladong——day80

    2024-01-08 15:38:03       33 阅读
  3. labuladong——day81

    2024-01-08 15:38:03       39 阅读
  4. labuladong——day84

    2024-01-08 15:38:03       36 阅读
  5. labuladong——day89

    2024-01-08 15:38:03       33 阅读
  6. labuladong——day68

    2024-01-08 15:38:03       39 阅读
  7. labuladong——day67

    2024-01-08 15:38:03       34 阅读
  8. labuladong——day69

    2024-01-08 15:38:03       37 阅读
  9. labuladong——day66

    2024-01-08 15:38:03       33 阅读
  10. labuladong——day70

    2024-01-08 15:38:03       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-08 15:38:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-08 15:38:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-08 15:38:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-08 15:38:03       20 阅读

热门阅读

  1. 2024.1.7力扣每日一题——赎金信

    2024-01-08 15:38:03       42 阅读
  2. 【Leetcode】707. 设计链表

    2024-01-08 15:38:03       40 阅读
  3. PHP知识点复习

    2024-01-08 15:38:03       34 阅读
  4. 优化Vue首页加载速度的实用方法

    2024-01-08 15:38:03       52 阅读
  5. C++ stack用法总结

    2024-01-08 15:38:03       49 阅读
  6. 完成python+neo4j+django踩坑记录

    2024-01-08 15:38:03       42 阅读
  7. python-日志模块以及实际使用设计

    2024-01-08 15:38:03       41 阅读
  8. 【Unity】动态申请权限

    2024-01-08 15:38:03       61 阅读
  9. Unity游戏引擎的未来进化展望

    2024-01-08 15:38:03       33 阅读