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. 【Weiss】【第03章】练习3.7:有序多项式相乘

    [练习3.7] 编写一个函数将两个多项式相乘,用一个链表实现.你必须保证输出的多项式按幂次排列,并且任意幂次最多只有一项. a.给出以O(M2N2)时间求解该问题的算法. b.写一个以O(M2N)时间 ...

  2. hdu2087kmp模板练习

    题目网址:http://icpc.njust.edu.cn/Problem/Hdu/2087/ 代码: #include<bits/stdc++.h> using namespace st ...

  3. wr720n v4 折腾笔记(五):终篇-编译安装openwrt附带njit

    前言: 由于没有多余的U盘,所以想直接压缩生成一个带njit-client的openwrt固件.其中按照网上的教程走,遇到了一些问题,这里前面的步骤来源于网络.后面给出处理问题的方法. 一.准备工作 ...

  4. 一个使用fasttext训练的新闻文本分类器/模型

    fastext是什么? Facebook AI Research Lab 发布的一个用于快速进行文本分类和单词表示的库.优点是很快,可以进行分钟级训练,这意味着你可以在几分钟时间内就训练好一个分类模型 ...

  5. SQL server 2012安装教程

    转自:https://blog.csdn.net/u013162035/article/details/78567389 注]博主使用的是SQL Server2012 其他版本的安装类似.[第一步]下 ...

  6. Python第五章-内置数据结构03-元组

    Python 内置的数据结构 三.元组(tuple) python 作为一个发展中的语言,也提供了其他的一些数据类型. tuple也是 python 中一个标准的序列类型. 他的一些操作和str和li ...

  7. 证明与计算(7): 有限状态机(Finite State Machine)

    什么是有限状态机(Finite State Machine)? 什么是确定性有限状态机(deterministic finite automaton, DFA )? 什么是非确定性有限状态机(nond ...

  8. 科普 | ​生成对抗网络(GAN)的发展史

    来源:https://en.wikipedia.org/wiki/Edmond_de_Belamy 五年前,Generative Adversarial Networks(GANs)在深度学习领域掀起 ...

  9. IO 流

    ------------恢复内容开始------------ 一.流的简介 输入流:把硬盘中的数据读取到内存中 输出流:把内存中的数据读取到硬盘中 1字符=2字节 1字节=8位 1.2顶级父类  二. ...

  10. .NET Core项目部署到Linux(Centos7)(七)启动和停止.NET Core项目

    目录 1.前言 2.环境和软件的准备 3.创建.NET Core API项目 4.VMware Workstation虚拟机及Centos 7安装 5.Centos 7安装.NET Core环境 6. ...