代码随想录day_13打卡

①、滑动窗口最大值

给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。

返回 滑动窗口中的最大值 

力扣(LeetCode)链接:https://leetcode.cn/problems/sliding-window-maximum/description/

 

事例:
输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:
滑动窗口的位置                最大值
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7

 

思路:

        这道题的滑动窗口可由队列来保持,但普通队列没法记录最大值,而优先级队列无法正确弹出窗口中最左边的数,故需要设计一个队列,可以直接获得最大值,并且保持窗口原样。

        特殊队列的实现思路:窗口中若出现比队首元素还大的值,则全部弹出并压入该值,小于队首元素则直接压入,若要弹出的数(窗口中最左边的数)等于队首元素,则队列弹出首元素。即该队列维持元素从大到小排序,队首元素一直保持着最大数。

MyQueue 代码:

class MyQueue{
    private Deque<Integer> deque;
    public MyQueue(){
        deque = new LinkedList<>();
    }

    void pop(int value){
        if(!deque.isEmpty() && value == deque.peek()){
            deque.poll();
        }
    }

    void push(int value){
        while(!deque.isEmpty() && value > deque.getLast()){
            deque.removeLast();
        }
        deque.add(value);
    }

    int front(){
        return deque.peek();
    }
}

题目代码:

public int[] maxSlidingWindow(int[] nums, int k) {
        int[] res = new int[nums.length - k + 1];
        MyQueue myQueue = new MyQueue();
        for (int i = 0; i < k; i++) {
            myQueue.push(nums[i]);
        }
        int count = 0;
        for (int i = k; i < nums.length; i++) {
            res[count++] = myQueue.front();
            myQueue.push(nums[i]);
            myQueue.pop(nums[i - k]);
        }
        res[count] = myQueue.front();
        return res;
    }

②、前k个高频元素

给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。

力扣(LeetCode)链接:https://leetcode.cn/problems/top-k-frequent-elements/description/

事例:

输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]

思路:

        这道题要输出前k个高频元素,故我们可以使用堆结构,即优先级队列排序。创建一个优先级队列(小根堆),填入前k个元素和频数,接下来的元素频数需跟堆首元素比较,若大于堆首元素,则弹出堆首元素,再压入目标元素,遍历到最后,堆的个数为k,保留了前k个高频元素。

        堆中既要保存元素和对应的频数(堆内元素可以为数组),故需要一个map映射,当map保存完数组中对应的数值后,遍历map,得到前k个高频元素。

代码:

public int[] topKFrequent(int[] nums, int k) {
        int[] res = new int[k];
        Map<Integer,Integer> map = new HashMap<>();
        PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });
        for(int x : nums){
            int count = map.getOrDefault(x,0);
            map.put(x,count + 1);
        }
        Set<Integer> integers = map.keySet();
        for(int x : integers){
            int count = map.get(x);
            if(queue.size() < k){
                queue.offer(new int[]{x,count});
            }else{
                if(queue.peek()[1] < count){
                    queue.poll();
                    queue.offer(new int[]{x,count});
                }
            }
        }
        for (int i = 0; i < k; i++) {
            res[k - 1 - i] = queue.poll()[0];
        }
        Arrays.sort(res);
        return res;
    }

代码随想录 (programmercarl.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值