码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • [JavaScript 刷题] 堆 - 数据流中的第 K 大元素, leetcode 703


    [JavaScript 刷题] 堆 - 数据流中的第 K 大元素, leetcode 703

    github repo 地址: https://github.com/GoldenaArcher/js_leetcode,Github 的目录 大概 会更新的更勤快一些。

    题目地址:703. Kth Largest Element in a Stream

    题目

    如下:

    Design a class to find the k^th largest element in a stream. Note that it is the k^th largest element in the sorted order, not the k^th distinct element.

    Implement KthLargest class:

    • KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.
    • int add(int val) Appends the integer val to the stream and returns the element representing the k^th largest element in the stream.

    解题思路

    整体来说没有什么特别难的地方,用 Min Heap 或者 BST 去解这道题都可以,不过因为是 leectode 上出现的这道题,最近又发现了一个 leetcode 中已经实现的 JavaScript 特有的 Heap 类,因此就在这里放一下了。

    只在 leetcode 上刷题的,可以看一下这个类:MaxPriorityQueue 以及对应的 MinPriorityQueue,目前看到实现的功能有:

    • constructor

      constructor 的用法如下:

      this.pq = new MaxPriorityQueue({ priority: (x) => -x });
      
      • 1

      其中的 priority 就是一个 comparator,这样的话保存一些特殊的结点也是可以实现的。

    • enqueue

    • dequeue

    • front

    • back

    使用 JavaScript 解题

    /**
     * @param {number} k
     * @param {number[]} nums
     */
    var KthLargest = function (k, nums) {
      this.pq = new MaxPriorityQueue({ priority: (x) => -x });
      this.size = k;
    
      for (const num of nums) {
        this.pq.enqueue(num);
    
        if (this.pq.size() > this.size) {
          // console.log(this.pq.dequeue());
          this.pq.dequeue();
        }
      }
    };
    
    /**
     * @param {number} val
     * @return {number}
     */
    KthLargest.prototype.add = function (val) {
      this.pq.enqueue(val);
      if (this.pq.size() > this.size) {
        this.pq.dequeue();
        // console.log(this.pq.dequeue());
      }
      return this.pq.front().element;
    };
    
    /**
     * Your KthLargest object will be instantiated and called as such:
     * var obj = new KthLargest(k, nums)
     * var param_1 = obj.add(val)
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
  • 相关阅读:
    短信视频提取批量工具,免COOKIE,博主视频下载抓取,爬虫
    数据分析-Pandas数据的探查面积图
    python读取json文件并转换成list存为json,解决存取json中文乱码问题
    HarmonyOS-静态库(SDK)的创建和使用
    排序算法基础篇
    不懂 Kubernetes 实现云原生是什么体验?
    073:vue+openlayers拖拽实现放大所选区域 (DragZoom示例代码)
    C语言加密字符(ZZULIOJ1064:加密字符)
    大前端 - 泛客户端 - Electron
    Java 实现简易计算器
  • 原文地址:https://blog.csdn.net/weixin_42938619/article/details/125468842
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号