[JavaScript] 优先队列

2023-05-16

JavaScript 实现优先队列

代码如下:

class Heap {
    constructor(cmp = (a, b) => a < b) {
        this.arr = new Array();
        this.cmp = (a, b) => cmp(a, b) //比较函数
    }

    // 大小
    size() {
        return this.arr.length;
    }

    // 判空
    empty() {
        return this.size() == 0;
    }

    // 堆顶数据
    top() {
        return this.arr[0];
    }

    // 入队 
    push(val) {
        this.arr.push(val);
        this.shiftUp();
    }

    // 向上调整
    shiftUp() {
        let now = this.arr.length - 1;
        while (now > 0) {
            let t = (now - 1) >> 1;
            if (this.cmp(this.arr[now], this.arr[t])) {
                [this.arr[now], this.arr[t]] = [this.arr[t], this.arr[now]];
                now = t;
            }
            else break;
        }
    }

    // 出队
    pop() {
        if (this.size() == 1) return this.arr.pop();
        const res = this.arr[0];
        this.arr[0] = this.arr.pop();
        this.shiftDown();
        return res;
    };

    // 向下调整
    shiftDown() {
        let now = 0;
        while (now * 2 + 1 < this.size()) {
            let t = now * 2 + 1;
            if (t + 1 < this.size() && this.cmp(this.arr[t + 1], this.arr[t])) t++;
            if (this.cmp(this.arr[t], this.arr[now])) {
                [this.arr[now], this.arr[t]] = [this.arr[t], this.arr[now]];
                now = t;
            }
            else break;
        }
    }
}

例题:

​​​​​​力扣 743. 网络延迟时间https://leetcode.cn/problems/network-delay-time/

function node(a, b) {
    this.first = a;
    this.second = b;
}

var networkDelayTime = function (times, n, k) {
    const edges = Array.from(new Array(n + 1), () => new Array());
    for (const pair of times) {
        edges[pair[0]].push(new node(pair[1], pair[2]));
    }
    const dis = new Array(n + 1).fill(10001);
    const visited = new Array(n + 1).fill(false);
    const heap = new Heap((a, b) => a.first < b.first);
    dis[k] = 0;
    heap.push(new node(0, k));
    while (heap.size() > 0) {
        const now = heap.pop().second;
        if (visited[now]) continue;
        visited[now] = true;
        for (const edge of edges[now]) {
            const next = edge.first;
            const w = edge.second;
            if (dis[next] > dis[now] + w) {
                dis[next] = dis[now] + w;
                heap.push(new node(dis[next], next));
            }
        }
    }
    let res = 0;
    for (let i = 1; i <= n; i++) {
        if (dis[i] == 10001) return -1;
        res = Math.max(res, dis[i]);
    }
    return res;
};

// 优先队列
class Heap {
    constructor(cmp = (a, b) => a < b) {
        this.arr = new Array();
        this.cmp = (a, b) => cmp(a, b) //比较函数
    }

    // 大小
    size() {
        return this.arr.length;
    }

    // 判空
    empty() {
        return this.size() == 0;
    }

    // 堆顶数据
    top() {
        return this.arr[0];
    }

    // 入队 
    push(val) {
        this.arr.push(val);
        this.shiftUp();
    }

    // 向上调整
    shiftUp() {
        let now = this.arr.length - 1;
        while (now > 0) {
            let t = (now - 1) >> 1;
            if (this.cmp(this.arr[now], this.arr[t])) {
                [this.arr[now], this.arr[t]] = [this.arr[t], this.arr[now]];
                now = t;
            }
            else break;
        }
    }

    // 出队
    pop() {
        if (this.size() == 1) return this.arr.pop();
        const res = this.arr[0];
        this.arr[0] = this.arr.pop();
        this.shiftDown();
        return res;
    };

    // 向下调整
    shiftDown() {
        let now = 0;
        while (now * 2 + 1 < this.size()) {
            let t = now * 2 + 1;
            if (t + 1 < this.size() && this.cmp(this.arr[t + 1], this.arr[t])) t++;
            if (this.cmp(this.arr[t], this.arr[now])) {
                [this.arr[now], this.arr[t]] = [this.arr[t], this.arr[now]];
                now = t;
            }
            else break;
        }
    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[JavaScript] 优先队列 的相关文章

随机推荐

  • PyTorch——应用一个已训练好的图片分类网络——AlexNet

    1 识别一个图像主体的预训练网络 ImageNet数据集是由一个Stanford大学维护的包含1400多万幅图像的非常大的数据集 xff0c 所有的图像都用来自WordNet数据集的名词层次结构标记 xff0c 而WordNet数据集又是一
  • 2020-09-19

    假设我们现在对 6 1 2 7 9 3 4 5 10 8 这个10个数进行排序 首先在这个序列中随便找一个数作为基准数 xff08 不要被这个名词吓到了 xff0c 就是一个用来参照的数 xff0c 待会你就知道它用来做啥的了 xff09
  • Pytorch——生成式对抗网络的实例

    1 GAN 生成器的最终目标是要欺骗判别器 xff0c 混淆真伪图像 xff1b 而判别器的目标是发现他何时被欺骗了 xff0c 同时告知生成器在生成图像的过程中可识别的错误 注意无论是判别器获胜还是生成器获胜 xff0c 都不是字面意义上
  • 哈工大面向服务的软件系统 期末开卷提纲

    引言 本课程期末考试为开卷 xff0c 博主2022年期末卷面94 100 xff0c 总分92 9排名第2 82 xff0c 现分享复习提纲以供学弟学妹们参考 本提纲仅供参考 xff0c 切勿进行其他目的的使用 基于2021秋季考试题的思
  • 编译原理——语法制导翻译

    一 概述 语法制导翻译主要是使用上下文无关文法来引导对语言的翻译 语义分析的结果通常就表现为中间代码 xff0c 因此 xff0c 语义分析和中间代码生成在一起称为语义翻译 语法分析的同时直接进行语义分析 xff0c 成为语法制导翻译 将语
  • 关闭指定端口号的进程

    一 引言 做数据库实验 xff0c 端口号8080老被占用 xff0c 导致IDEA Debug模式起不来 二 杀进程 1 查端口号被哪个傻逼进程占用了 netstat ano findstr 34 端口号 34 2 把这个傻逼进程杀掉 t
  • 批处理dos命令深度递归扫描删除指定路径下指定文件夹的内容

    1 先列印将要被清理的指定路径下的所有logs文件夹 2 列印后按任意键继续执行则会删除 如果要扫描其它文件夹名 xff0c 修改批处理中的logs为要清理的文件夹名即可 64 echo off 配置需要扫描清理文件夹的路径 set del
  • 惯性导航系统(INS)与全球卫星定位系统(GPS)

    惯性导航系统 xff08 INS xff09 与全球卫星定位系统 xff08 GPS xff09 来源 xff1a 中国自动化网 作者 xff1a 发表时间 xff1a 2010 06 30 08 26 00 1 摘要 目前飞行器所使用的导
  • 关于git clone的时候,如果他一直提示你You do not have permission to pull from the repository 解决方案

    其实很简单 控制面板 所有控制面板项 凭据管理器 添加普通凭据 git https gitee com 注意地址写这个就好了 用户名 密码就是你的用户名密码
  • 线刷rom时fastboot flash system system.img报错

    我在线刷Nexus9的谷歌官方系统时遇到如下报错 fastboot flash system system img Sending sparse 39 system 39 1 3 501966 KB FAILED Error reading
  • 北京科技大学通用学术英语Mooc作文 大一下(20级版)

    注 xff1a 所选用文章均来自20级学生 xff0c 不是范文 xff0c 仅供参考 xff0c 想拿高分的还是自行动笔为上 xff08 文章整体预估得分区间为85 95 xff09 大一下 U1 an introductory para
  • verilog 学号输入并滚动显示

    TOP module module top input clk100mhz input clr input s input key1 input key2 input key3 input key4 input 2 0 count1 out
  • 生产者消费者模型详解

    生产者消费者模型 文章目录 生产者消费者模型什么是生产者消费者模型基于BlockingQueue的生产者消费者模型单生产者单消费者模型多生产者多消费者模型 什么是生产者消费者模型 生产者消费者模式就是通过一个容器来解决生产者和消费者的强耦合
  • Verilog 电子秤设计

    能跑就行系列 仅供参考 xff08 xff09 功能 单次计价 xff1a 输入物品的重量 单价 xff0c 显示物品的总价 xff08 61 重量 单价 xff09 累计计价 xff1a 第一次按下累计按键 xff0c 记住当前物品的总价
  • Verilog 四层电梯设计

    能跑就行系列 描述 xff1a 设计一个4层楼的电梯控制系统 xff0c 完成电梯向上或向下移动到被请求楼层 xff08 假设电梯每移动一层需要1秒 xff09 请求可来自每层楼的呼叫按钮 xff0c 也可来自电梯内的目的楼层选择 当电梯到
  • 北京科技大学 工科物理实验 大二上

    前言 本文由20级学生整理 xff0c 包括实验目的和仪器 实验原理 实验步骤三个部分 主要是想节约一下大家手机拍照扫描 语音输入或手打的时间 xff08 可能有些任课老师要求手写 xff0c 那就爱莫能助了 xff09 使用方法 点击 源
  • 北京科技大学 工科物理实验 大二下

    前言 本文由20级学生整理 xff0c 包括实验目的和仪器 实验原理 实验步骤三个部分 主要是想节约一下大家手机拍照扫描 语音输入或手打的时间 xff08 可能有些任课老师要求手写 xff0c 那就爱莫能助了 xff09 5 4 实验原理部
  • [操作系统]进程同步 Reader-Writer问题 共享缓冲区问题 面包师问题 吸烟者问题

    目录 1 Reader Writer问题 2 共享缓冲区问题 3 面包师问题 4 吸烟者问题 1 Reader Writer问题 多个Reader进程 xff0c 多个Writer进程 xff0c 共享文件F 允许多个Reader进程同时读
  • 北京科技大学 算法分析与设计 计蒜客

    北京科技大学 算法分析与设计 计蒜客平时作业及实验代码 2022年 xff08 仅供参考 xff09 目录 作业一 买房子 USACO Open08 农场周围的道路 国王的魔镜 作业二 蜗牛旅游 NOIP2001 求先序排列 作业三 二分查
  • [JavaScript] 优先队列

    JavaScript 实现优先队列 代码如下 xff1a class Heap constructor cmp 61 a b 61 gt a lt b this arr 61 new Array this cmp 61 a b 61 gt