【剑指Offer系列】59-队列的最大值(滑动窗口、优先队列)

题目:队列的最大值
举例:输入数组{2,3,4,2,6,2,5,1},和滑动窗口的大小3,输出六个滑动窗口{2,3,4}、{3,4,2}、{4,2,6}、{2,6,2}、{6,2,5}、{2,5,1}的最大值分别为{4,4,6,6,6,5}
实现:复杂度为O(数组大小*滑动窗口大小)

通过队列存储array的元素,每一次,弹出一个最先进来的,然后加入一个新的,队列的大小始终为3,每次从队列中找出最大值即可

public class Main59 {
    public static void main(String[] args) {
        int[] array = {2,3,4,2,6,2,5,1};
        Scanner sc = new Scanner(System.in);
        maxValueOfQueue(array,sc.nextInt());
    }

    private static void maxValueOfQueue(int[] array, int n) {
        if (array == null || array.length == 0 || n <= 0)
            return;
        Queue<Integer> queue = new LinkedBlockingQueue<>();
        for (int j = 0; j < n; j++) {
            queue.add(array[j]);
        }
        int[] maxArr = new int[n];
        for (int i = n; i < array.length; i++) {
            //此次
            System.out.println(max(queue,maxArr));
            //下次
            //先弹出queue
            queue.poll();
            //再添
            queue.add(array[i]);
        }
        System.out.println(max(queue,maxArr));//最后一次2,5,1
    }

    private static int max(Queue<Integer> queue, int[] maxArr) {
        int index = 0;
        for (Integer integer : queue) {
            maxArr[index++] = integer;
        }
        return Arrays.stream(maxArr).max().getAsInt();
    }
}

思想2:使用优先队列

1、求最大值:大顶堆
2、在用到最大值的同时,又需要最大值对应的index,即list.indexOf此功能

        Queue<Pair<Integer, Integer>> queue = new PriorityQueue<>(
                 Comparator.comparingInt((p) -> -p.getKey())
        );

3、先根据key逆序,再根据value逆序
第一个功能:按照Pair.getKey()进行大顶堆的定义
第二个功能,当两个Pair的key值相同时,再按照value值进行降序排序。

        Queue<Pair<Integer, Integer>> queue = new PriorityQueue<>(
                 Comparator.comparingInt((Pair<Integer, Integer> p) -> -p.getKey())
                 // 如果 key 相同,则按照 value 降序
                .thenComparingInt((Pair<Integer, Integer> p) -> -p.getValue())
        );
code
public class MaxValueInQueue {
    public static void main(String[] args) {
        int[] arr = {9,10,9,-7,-4,-8,2,-6};
        int[] res = maxSlidingWindow(arr, 5);
        System.out.println(Arrays.toString(res));
    }

    public static int[] maxSlidingWindow(int[] arr, int k) {
        int i = 0;
        int[] res = new int[arr.length - k + 1];
        Queue<Pair<Integer, Integer>> queue = new PriorityQueue<>(
                 Comparator.comparingInt((p) -> -p.getKey())

        );

        for (int j = 0; j < k - 1; j++) {
            int val = arr[j];
            queue.add(new Pair<>(val, j));
        }
        // {9,10,9,-7,-4,-8,2,-6}  ==> [10,10,9,2]
        for (int j = k - 1; j < arr.length; j++) {
            int val = arr[j];
            queue.add(new Pair<>(val, j));
            Pair<Integer, Integer> maxPair = queue.peek();
            Integer maxVal = maxPair.getKey();
            Integer maxVaIndex = maxPair.getValue();
            // 必须是有效的最大值,现在是队列中的最大值,但是加入队列至今都是被别的更大值所遮盖的
            while (maxVaIndex < i) {
                maxPair = queue.poll();
                maxVal = maxPair.getKey();
                maxVaIndex = maxPair.getValue();
            }
            // 最大值,是左窗口,则需要剔除掉,不影响下一个滑动后的窗口
            if (i == maxVaIndex) {
                while (queue.size() > 0 && Objects.equals(maxVal, queue.peek().getKey())) {
                    queue.poll();
                }
            }
            res[i++] = maxVal;
        }
        return res;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值