剑指offer JZ59 滑动窗口的最大值

描述

剑指offer JZ59 滑动窗口的最大值
给定一个长度为 n 的数组 num 和滑动窗口的大小 size ,找出所有滑动窗口里数值的最大值。

例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。
数据范围: 1≤n≤100001≤n≤10000,0≤size≤100000≤size≤10000,数组中每个元素的值满足 ∣val∣≤10000∣val∣≤10000
窗口大于数组长度或窗口长度为0的时候,返回空。

方法一:优先队列

思路:
利用PriorityQueue可以自定义规则排序的性质,每次只需要从其中取出最大的元素即可。此方法简单易实现,但是性能没有方法二双端队列好。
具体实现及注释:

public class Solution {
   public ArrayList<Integer> maxInWindows (int[] num, int size) {
       ArrayList<Integer> list = new ArrayList<>();
       if (size == 0 || size > num.length) return list;
       //实例化从大到小排序的queue
       PriorityQueue<Integer> queue = new PriorityQueue<Integer>((o1, o2) -> o2 - o1);
       //将第一个窗口的元素加入queue
       for (int i = 0; i < size; i++) queue.add(num[i]);
       list.add(queue.peek());
       //双指针
       int start = 0; 
       int end = size;
       while (end < num.length) {
           queue.remove(num[start++]);
           queue.add(num[end++]);
           list.add(queue.peek());
       }
     
       return list;
   }
}

方法二:双端队列

思路:
使用一个双端队列,当有新元素添加时,从队列尾部向前开始判断,如果前面的元素小于新元素,则将其从队列中移除。并且在取最大元素时,判断在队首的元素是否需要移除(如果已经不在滑动窗口内则需要移除)。性能比优先队列要好

// 双向队列法
public class Solution {
    public ArrayList<Integer> maxInWindows (int[] nums, int size) {
        ArrayList<Integer> ret = new ArrayList<>();
        int length = nums.length;
        if (length < size || size == 0) return ret;
        //双端队列,存储元素的下标
        Deque<Integer> queue = new ArrayDeque<>();
        for (int i = 0; i < length; i++) {
            //如果队尾的值小于新元素,则出队列
            while (!queue.isEmpty() && nums[queue.peekLast()] < nums[i]){
                queue.pollLast();
            }
            queue.offerLast(i);
            //如果队首的值过期,则出队列
            if (queue.peekFirst() == i-size) {
                queue.pollFirst();
            }
            if (i >= (size- 1)) ret.add(nums[queue.peekFirst()]);
        }
        return ret;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值