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

Your KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.

Example:

int k = 3;
int[] arr = [4,5,8,2];
KthLargest kthLargest = new KthLargest(3, arr);
kthLargest.add(3);   // returns 4
kthLargest.add(5);   // returns 5
kthLargest.add(10);  // returns 5
kthLargest.add(9);   // returns 8
kthLargest.add(4);   // returns 8

Note: 
You may assume that nums' length ≥ k-1 and k ≥ 1.

这道题让我们在数据流中求第K大的元素,跟之前那道Kth Largest Element in an Array很类似,但不同的是,那道题的数组是确定的,不会再增加元素,这样确定第K大的数字就比较简单。而这道题的数组是不断在变大的,所以每次第K大的数字都在不停的变化。那么我们其实只关心前K大个数字就可以了,所以我们可以使用一个最小堆来保存前K个数字,当再加入新数字后,最小堆会自动排序,然后把排序后的最小的那个数字去除,则堆中还是K个数字,返回的时候只需返回堆顶元素即可,参见代码如下:

解法一:

class KthLargest {
public:
KthLargest(int k, vector<int> nums) {
for (int num : nums) {
q.push(num);
if (q.size() > k) q.pop();
}
K = k;
} int add(int val) {
q.push(val);
if (q.size() > K) q.pop();
return q.top();
} private:
priority_queue<int, vector<int>, greater<int>> q;
int K;
};

我们也可以使用multiset来做,利用其可重复,且自动排序的功能,这样也可以达到最小堆的效果,参见代码如下:

解法二:

class KthLargest {
public:
KthLargest(int k, vector<int> nums) {
for (int num : nums) {
st.insert(num);
if (st.size() > k) st.erase(st.begin());
}
K = k;
} int add(int val) {
st.insert(val);
if (st.size() > K) st.erase(st.begin());
return *st.begin();
} private:
multiset<int> st;
int K;
};

类似题目:

Kth Largest Element in an Array

参考资料:

https://leetcode.com/problems/kth-largest-element-in-a-stream

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素的更多相关文章

  1. Leetcode703.Kth Largest Element in a Stream数据流中的第K大元素

    设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包含数据流中 ...

  2. 215 Kth Largest Element in an Array 数组中的第K个最大元素

    在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假 ...

  3. leetcode Kth Largest Element in a Stream——要熟悉heapq使用

    703. Kth Largest Element in a Stream Easy Design a class to find the kth largest element in a stream ...

  4. LeetCode - Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  5. Python3解leetcode Kth Largest Element in a Stream

    问题描述: Design a class to find the kth largest element in a stream. Note that it is the kth largest el ...

  6. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  7. Leetcode 703. 数据流中的第K大元素

    1.题目要求 设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器, ...

  8. [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  9. 【LeetCode】703. Kth Largest Element in a Stream 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

随机推荐

  1. DirectX11 With Windows SDK--13 动手实现一个简易Effects框架、阴影效果绘制

    前言 到现在为止,所有的教程项目都没有使用Effects11框架类来管理资源.因为在D3DCompile API (#47)版本中,如果你尝试编译fx_5_0的效果文件,会收到这样的警告: X4717 ...

  2. tomcat7的安装与maven安装

    tomcat7的安装与配置: 下载tomcat7 :wget 地址 解压:tar -zxvf 文件名 编辑tomcat目录下的conf下的server.xml文件: <Connector por ...

  3. swagger-ui中测试接口(enum传值) 报400错误

    swagger-ui中测试接口(enum传值) 报400错误 PriceRuleController: @PostMapping("/update") @ApiOperation( ...

  4. sass 工具库

    github : https://github.com/uustoboy/base_mixins ( 有坑慎用 ) 项目截图: _base_mixins.scss 混合宏的引入文件; _setting ...

  5. 【原创】Java基础之常用JVM工具

    查看当前所有java进程 # jps 查看某个进程的堆内存占用情况 # jmap -heap $pid 查看某个进程的堆内存中对象分布情况 # jmap -histo $pid 将某个进程的堆内存导出 ...

  6. 字符串(2)KMP算法

    给你两个字符串a(len[a]=n),b(len[b]=m),问b是否是a的子串,并且统计b在a中的出现次数,如果我们枚举a从什么位置与匹配,并且验证是否匹配,那么时间复杂度O(nm), 而n和m的范 ...

  7. Windows Internals 笔记——作业

    1.Windows提供了一个作业内核对象,它允许我们将进程组合在一起并创建一个“沙箱”来限制进程能够做什么.创建只包含一个进程的作业同样非常有用,因为这样可以对进程施加平时不能施加的限制. 2.如果进 ...

  8. numpy的基础运算2-【老鱼学numpy】

    numpy的基础运算中还有很多运算,我们这里再记录一些. 最小/大值索引 前面一篇博文中我们讲述过如何获得数组中的最小值,这里我们获得最小/大值的索引值,也就是这个最小/大值在整个数组中位于第几位. ...

  9. 一步步建立 Vue + Cesium 初始化项目

    一步步建立 Vue + Cesium 初始化项目 初始化 Vue 项目 升级 npm npm install -g npm 安装 @vue/cli 和 @vue/cli-service-global ...

  10. Python3中的真值测试

    1. 真值测试 所谓真值测试,是指当一种类型对象出现在if或者while条件语句中时,对象值表现为True或者False.弄清楚各种情况下的真值对我们编写程序有重要的意义. 对于一个对象a,其真值定义 ...