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. 横向滚动布局 white-space:nowrap

    float + 两层DOM实现 html <div class="container"> <div class="div1 clearfix" ...

  2. 队列优化的dijkstra

    #include<iostream> #include<queue> #include<cstdio> #include<cstring> #inclu ...

  3. httpClient4.5 closeableHttpClient用法

    HttpClient一 简介1.尽管java.net包提供了基本通过HTTP访问资源的功能,但它没有提供全面的灵活性和其它很多应用程序需要的功能.HttpClient就是寻求弥补这项空白的组件,通过提 ...

  4. JN_0006:MongoDB未授权访问漏洞处理

    开启MongoDB服务时不添加任何参数时,默认是没有权限验证的,登录的用户可以通过默认端口无需密码对数据库任意操作而且可以远程访问数据库. 2.[修复建议]:临时方案:配置AUTH,做好访问认证.打开 ...

  5. JavaScript 基本类型和引用类型

    前言 ECMAScript变量可能包含两种不同数据类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据段,而引用类型值指那些可能由多个值构成的对象. 基本类型 Undefined.Null.B ...

  6. UE4 AR开发笔记

    1.基础使用 ArToolKit:生成图片特征,可以用彩图.(图片先灰化)    genTexData效准相机.由于有的相机照相有弧度.  calib_camera 2.使用UE4ARPlugins做 ...

  7. 基于百度API+Flask实现网页版和图灵机器聊天

    开发前准备 调用百度和图灵机器人相关的 参考链接:www.cnblogs.com/changtao/p/10596385.html 下载一个网页录音的js插件 链接:https://pan.baidu ...

  8. 《尚学堂_史上最易懂的设计模式视频》--章节5 动态代理-JDK6自带的编译器

    所有的设计模式中最难的一个 ==组合和聚合是有很大区别的 组合和聚合是有很大区别的,这个区别不是在形式上,而是在本质上: 比如A类中包含B类的一个引用b,当A类的一个对象消亡时,b这个引用所指向的对象 ...

  9. C# - 设计模式 - 策略模式

    策略模式 问题场景 多个类型都有一些共同的属性和方法,可以称这些成员为行为,为了避免重复在多个类型中编码相同部分的行为,应考虑将这些行为定义在抽象类(超类)中,利用继承时多个类型可以共享这些行为.比如 ...

  10. 第一周——数据分析之表示 —— Numpy 数据存取与函数

    数据的CSV文件的存取 CSV文件:CSV (Comma‐Separated Value, 逗号分隔值) CSV是一种常见的文件格式,用来存储批量数据 np.savetxt(frame, array, ...