leetcode 703. Kth Largest Element in a Stream & c++ priority_queue & minHeap/maxHeap
703. Kth Largest Element in a Stream & c++ priority_queue & minHeap/maxHeap
相关链接
leetcode
c++ priority_queue cplusplus
c++ priority_queue cnblog
背景知识
堆是算法中常用的数据结构之一,其结构是完全二叉树,但实现的方法最常见的是使用数组;这里主要介绍小顶堆,其根元素最小,对于任何一个节点来说,他都比其后代要小;访问器根元素的时间为O(1);树的高度严格控制在 log(n) 以内,故每次插入元素的时间为 O(log(n)).
在c++的STL中提供了现成的堆模板给我们使用;你需要引入头文件queue.h即可;
具体使用如下
#include<queue.h>
using namespace std;
int main()
{
//大顶堆
priority_queue<int> maxHeap;
//等价于
priority_queue<int, vector<int>, less<int> > maxHeap1;
//小顶堆
priority_queue<int, vector<int>, greater<int> > maxHeap1;
}
描述
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.
solution1
用一个小顶堆保存最大的K个数字;根节点处为K个数字中最小的一个,也是所有数据中第k大的数字;每当有新元素进来的时候,拿走小顶堆中最小的元素;
using namespace std;
class KthLargest {
public:
int k;
priority_queue<int, vector<int>, greater<int> > minHeap;
public:
KthLargest(int k, vector<int>& nums) :minHeap(nums.begin(), nums.end()) {
this->k = k;
}
int add(int val) {
minHeap.push(val);
while (k < minHeap.size())
minHeap.pop();
return minHeap.top();
}
};
int main()
{
priority_queue<int> maxHeap;
//priority_queue<int, vector<int>, less<int> > maxHeap;
priority_queue<int, vector<int>, greater<int> > minHeap;
int k = 3;
vector<int> arr = { 4,5,8,2 };
KthLargest kthLargest =KthLargest(3, arr);
cout<<kthLargest.add(3); // returns 4
cout << kthLargest.add(5); // returns 5
cout << kthLargest.add(10); // returns 5
cout << kthLargest.add(9); // returns 8
cout << kthLargest.add(4); // returns 8
system("pause");
}
leetcode 703. Kth Largest Element in a Stream & c++ priority_queue & minHeap/maxHeap的更多相关文章
- LeetCode - 703. 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 ...
- 【Leetcode_easy】703. Kth Largest Element in a Stream
problem 703. Kth Largest Element in a Stream 题意: solution1: priority_queue这个类型没有看明白... class KthLarg ...
- 【LeetCode】703. Kth Largest Element in a Stream 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- [LeetCode&Python] Problem 703. 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 ...
- 703. Kth Largest Element in a Stream
题目来源: https://leetcode.com/problems/kth-largest-element-in-a-stream/ 自我感觉难度/真实难度: 题意: 这个题目的意思解读了半天,没 ...
- 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 ...
- [LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 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 ...
随机推荐
- tensorflow一些API的基本理解
1.tf.Session self._session = None opts = tf_session.TF_NewSessionOptions(target=self._target, config ...
- Python习题集(十六)
每天一习题,提升Python不是问题!!有更简洁的写法请评论告知我! https://www.cnblogs.com/poloyy/category/1676599.html 题目 写一个函数repl ...
- Python下载各种功能包出问题
问题详情 点击之后出现 AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader' 解决方法 c ...
- mysql 更换数据目录
mysql 更换数据目录 1.停止MySql服务: /etc/rc.d/init.d/mysql stop 或者 service mysql stop 2.确认MySql原来的数据目录,查找datad ...
- 【5min+】更好的选项实践。.Net Core中的IOptions
系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...
- win7系统下的Nodejs开发环境配置
此处不推荐使用msi安装包直接安装nodejs,我们应该知道它里面做了哪些事情,这样以后出问题的时候,可以更快速地定位问题点.另一方面,直接安装的情况,以后更新了版本的话会很麻烦,因为如果我们想体验新 ...
- 解决 Mac Android Studio Gradle Sync 慢的问题
1.启动Android Studio 2.从项目的 gradle/wrapper/gradle-wrapper.properties 目录中找到 distributionUrl 这个字段,查看后面对应 ...
- 插入排序与Shell排序
ElementType ShellSort( ElementType A[], int N ) { ;h>;h/=){ for(int i=h;i<N;i++){ ElementType ...
- Android之练习MVVM+DataBinding框架模式
最近简单学习了MVVM框架,记录一下. 结果演示: 分析其功能在不同框架下的构成: 无框架 可以明显感受到在无框架下,虽然一个单独的Activity即可实现功能,但其负担过重,代码复查时繁琐,一旦需要 ...
- Python第六章-函数01-函数的概念和使用
函数 为了便于程序的维护和更好的实现模块化,好的程序都会分解为很多函数. 可以这么说,对于任何的编程语言,函数都是一个非常重要的概念. python 不仅简化了函数的定义过程,而且还大量借鉴了其他函数 ...