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的更多相关文章

  1. 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 ...

  2. 【Leetcode_easy】703. Kth Largest Element in a Stream

    problem 703. Kth Largest Element in a Stream 题意: solution1: priority_queue这个类型没有看明白... class KthLarg ...

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

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

  4. [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 ...

  5. 703. Kth Largest Element in a Stream

    题目来源: https://leetcode.com/problems/kth-largest-element-in-a-stream/ 自我感觉难度/真实难度: 题意: 这个题目的意思解读了半天,没 ...

  6. 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 ...

  7. [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 ...

  8. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  9. 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 ...

随机推荐

  1. 在Centos系统中基于PowerDNS和Poweradmin自建域名解析服务器替代DnsPod

    本文讲述了我在Centos 7系统(其他版本的Centos未尝试)中基于PowerDNS和poweradmin自建域名解析服务器替代DnsPod的过程.通过本文所述方法,可以建立权威域名解析服务器的m ...

  2. 关于编码和解码问题——encode、decode

            一.背景和问题 近期在做一个关于声卡录音的项目,开发环境是win10 64位家庭中文版,pycharm2019.1,python3.6(Anaconda3),python模块pyaud ...

  3. Natas29 Writeup(Perl命令注入、00截断、绕过过滤)

    Natas29: 本关打开后,可以看到一个下拉列表,选择不同的内容,会得到不同的大量文本的页面. 观察url部分:http://natas29.natas.labs.overthewire.org/i ...

  4. golang超级mapper包 - coven

    coven介绍 你可以把它理解成.NET 的 AutoMapper,java的modelmapper 一个快速的转换器去,支持结构到结构,切片到切片和映射到映射非反射转换,类型与嵌套指针支持. 不支持 ...

  5. 内置函数---filter和map

    filter filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条件的元素,返回 ...

  6. WSL下卸载了zsh / fish后无法启动bash解决方案

    最近在鼓捣wsl,感觉自己用还是蛮好用的.听说1903要更新新的cmd,还蛮期待的 解决步骤: 建议先下载个everything , windows下非常好用的文件查找软件. 启动后搜索.bashrc ...

  7. python浅学【网络服务中间件】之MongoDB

    一.关于MongoDB: MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供 ...

  8. Netty中ChannelHandler的生命周期

    在使用Netty进行网络编程的时候,通常需要在网络连接的不同阶段进行相应的操作,比如在连接建立时,客户端向服务端发起认证,在接收到数据时对数据内容进行解析等等.那么,连接的不同阶段在netty中如何表 ...

  9. 服务器模型---socket!!!

    /*********************服务器模型******************/ 一.循环服务器:循环服务器在同一时刻只可以相应一个客户端请求: 二.并发服务器:并发服务器在同一时刻可以相 ...

  10. Linux时间和现实时间不同步解决方案

    输入三条命令 yum install ntpdate -y ntpdate tiger.sina.com.cnping tiger.sina.com.cn 然后输入date检查时间是否已经同步