代码随想录训练营第十三天|239. 滑动窗口最大值 ● 347.前 K 个高频元素

239. 滑动窗口最大值

题目链接:https://leetcode.cn/problems/sliding-window-maximum/

var maxSlidingWindow = function(nums, k) {
class MonoQuene{
    queue;
    // 用于创建并初始化一个对象
    constructor(){
        this.queue=[]
    }
    enqueue(value){
        let back=this.queue[this.queue.length-1]
        while(back!==undefined&&back<value){
            this.queue.pop() //只保留最大的数在队列口
            back=this.queue[this.queue.length-1]
        }
        this.queue.push(value)
    }
    dequeue(value){
        let front=this.front()
        if(front===value){
            this.queue.shift() //把数组中的第一个元素从中删除并返回第一个元素的值
        }
    }
    front(){
        return this.queue[0]
    }
}
let helperQueue=new MonoQuene()
let i=0,j=0
let resArr=[]
while(j<k){
    helperQueue.enqueue(nums[j++])
}
resArr.push(helperQueue.front())
while(j<nums.length){
    helperQueue.enqueue(nums[j])
    helperQueue.dequeue(nums[i])
    resArr.push(helperQueue.front())
    i++,j++
}
return resArr
};

看视频所学:

1.首先定义一个单调队列即维持队列单调递增或者单调递减

2.保持弹出一个元素,加入一个元素的原则。

3.若在队列的元素比即将要进入队列的元素小,即将队列中的元素全部弹出,将即将要进入队列的元素返回并加入队列中。反之,若即将要进入队列的元素比在队列出口处的元素小,则将其放入。

这道题代码比较难理解,需要下来好好多刷几遍才行。

347.前 K 个高频元素

题目链接:https://leetcode.cn/problems/top-k-frequent-elements/

// js 没有堆 需要自己构造
class Heap {
    constructor(compareFn) {
        this.compareFn = compareFn;
        this.queue = [];
    }

    // 添加
    push(item) {
        // 推入元素
        this.queue.push(item);

        // 上浮
        let index = this.size() - 1; // 记录推入元素下标
        let parent = Math.floor((index - 1) / 2); // 记录父节点下标

        while (parent >= 0 && this.compare(parent, index) > 0) { // 注意compare参数顺序
            [this.queue[index], this.queue[parent]] = [this.queue[parent], this.queue[index]];

            // 更新下标
            index = parent;
            parent = Math.floor((index - 1) / 2);
        }
    }

    // 获取堆顶元素并移除
    pop() {
        // 堆顶元素
        const out = this.queue[0];

        // 移除堆顶元素 填入最后一个元素
        this.queue[0] = this.queue.pop();

        // 下沉
        let index = 0; // 记录下沉元素下标
        let left = 1; // left 是左子节点下标 left + 1 则是右子节点下标
        let searchChild = this.compare(left, left + 1) > 0 ? left + 1 : left;

        while (searchChild !== undefined && this.compare(index, searchChild) > 0) { // 注意compare参数顺序
            [this.queue[index], this.queue[searchChild]] = [this.queue[searchChild], this.queue[index]];

            // 更新下标
            index = searchChild;
            left = 2 * index + 1;
            searchChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
        }

        return out;
    }

    size() {
        return this.queue.length;
    }

    // 使用传入的 compareFn 比较两个位置的元素
    compare(index1, index2) {
        // 处理下标越界问题
        if (this.queue[index1] === undefined) return 1;
        if (this.queue[index2] === undefined) return -1;

        return this.compareFn(this.queue[index1], this.queue[index2]);
    }

}

const topKFrequent = function (nums, k) {
    const map = new Map();

    for (const num of nums) {
        map.set(num, (map.get(num) || 0) + 1);
    }

    // 创建小顶堆
    const heap= new Heap((a, b) => a[1] - b[1]);

    // entry 是一个长度为2的数组,0位置存储key,1位置存储value
    for (const entry of map.entries()) {
        heap.push(entry);

        if (heap.size() > k) {
            heap.pop();
        }
    }

    // return heap.queue.map(e => e[0]);

    const res = [];

    for (let i = heap.size() - 1; i >= 0; i--) {
        res[i] = heap.pop()[0];
    }

    return res;
};

看完视频所学:

1.本题主要借助小顶堆来实现。所谓的小顶堆是什么呢?即堆头是最小的元素。

什么是堆呢?堆是一颗完全二叉树,树中每个节点的值都不小于其左右孩子的值。

注意点:

1.map.get()函数用来获取一个map对象中指定的元素

2.map.set()为map对象添加一个指定键(key)和值(value)的新元素。

3.map.has()返回一个boolean值,用来表示map中是否存在指定元素。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值