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. Python习题集(三)

    每天一习题,提升Python不是问题!!有更简洁的写法请评论告知我! https://www.cnblogs.com/poloyy/category/1676599.html 题目 写一个小程序:控制 ...

  2. 用Python算带有进度条的圆周率

    import time scale=50 print("执行开始".center(scale//2,"-")) start=time.perf_counter( ...

  3. postman使用简介

    postman进行Http类型的接口测试的功能测试(手工测试): 1.postman下载,解压,打开Chrome浏览器-->设置-->扩展程序-->勾选开发者模式-->加载已解 ...

  4. SpringMVC框架——常用注解

    @RequestMapping Spring MVC 通过 @RequestMapping 注解将请求与业务方法进行映射,在方法定义处,在类定义都可以添加该注解. 常用参数: 1.value:指定请求 ...

  5. Java流中的map算子和flatMap算子的区别

    map算子和flatMap算子 map和flatMap都是映射(转换),那么他们之间究竟有什么区别呢? 1.我们先简单了解下map算子: @org.junit.Test public void tes ...

  6. webpack打包es6代码

    1.简单描述一下es6的模块导入和导出的语法: //导出:export var aa = 10;export function demo(){} //不能写成:var aa = 10;export a ...

  7. Django HttpResponse

    HttpResponse 概述:给浏览器返回数据 HttpRequest对象是由django创建的,HttpResponse对象由程序员创建 用法 1:不调用模板,直接返回数据. 例: def get ...

  8. 【笔记3-26】Python语言基础

    编译型语言和解释型语言 编译型语言 C 先编译 解释型语言 Python 边执行边编译 Python的介绍 吉多·范罗苏姆 1991 解释型语言 Life is short you need Pyth ...

  9. TensorFlow 中文资源精选,官方网站,安装教程,入门教程,实战项目,学习路径。

    Awesome-TensorFlow-Chinese TensorFlow 中文资源全集,学习路径推荐: 官方网站,初步了解. 安装教程,安装之后跑起来. 入门教程,简单的模型学习和运行. 实战项目, ...

  10. 理解BERT:一个突破性NLP框架的综合指南

    概述 Google的BERT改变了自然语言处理(NLP)的格局 了解BERT是什么,它如何工作以及产生的影响等 我们还将在Python中实现BERT,为你提供动手学习的经验 BERT简介 想象一下-- ...