class KthLargest {
public:
KthLargest(int k, vector<int> nums) {
size = k;
for(auto num:nums){
pq.push(num);
if(pq.size() > size)
pq.pop();
}
} int add(int val) {
pq.push(val);
if(pq.size() > size)
pq.pop();
return pq.top();
}
private:
priority_queue<int, vector<int>, greater<int>> pq;
int size;
}; /**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/

leetcode703的更多相关文章

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

  2. Leetcode703.Kth Largest Element in a Stream数据流中的第K大元素

    设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包含数据流中 ...

  3. LeetCode703 流中第k大的元素

    前言: 我们已经介绍了二叉搜索树的相关特性,以及如何在二叉搜索树中实现一些基本操作,比如搜索.插入和删除.熟悉了这些基本概念之后,相信你已经能够成功运用它们来解决二叉搜索树问题. 二叉搜索树的有优点是 ...

随机推荐

  1. 读《the facebook effect》

    现在觉得时间越来越少,特别是抽出时间读书都感觉是一种奢侈. 今天把facebook读完了,想记录下自己的体会.不知该从什么地方写起.以前,曾注册过一个facebook帐号,一直没登过,好像从那时起,f ...

  2. 《DSP using MATLAB》示例Example 6.28

    代码: % The following 3 lines produce filter coefficients shown in Table 6.1 wp = [0.35, 0.65]; ws = [ ...

  3. REST服务开发实战【转】

    原文:http://kb.cnblogs.com/page/91827/ REST介绍 如果要说什么是REST的话,那最好先从Web(万维网)说起. 什么是Web呢?读者可以查看维基百科的词条(htt ...

  4. drone 学习四 几个有用的命令

    1. 安装cli 工具 linux curl -L https://github.com/drone/drone-cli/releases/download/v0.8.5/drone_linux_am ...

  5. Helm Charts

    Use this repository to submit official Charts for Kubernetes Helm. Charts are curated application de ...

  6. Arrays--codility

    lesson 2: Arrays exercise: Problem: Given array A consisting of N integers, return the reversed arra ...

  7. php查询mysql数据库 查询条件替中文字符串变量时无法查询

    $temp2 ='十年';mysql_query("SET NAMES GBK"); $res = mysql_query("select songer_name fro ...

  8. C语言中可变形参简单实例

    以下程序主要包括三个主要函数: 一个最简单的可变形参函数实例: 一个简单的printf功能的实例: 一个打印字符串函数(辅助): 其中myPrintf函数,实现了printf的部分简单功能,并没有去实 ...

  9. ehcache介绍

    EHCache是来自sourceforge(http://ehcache.sourceforge.net/) 的开源项目,也是纯Java实现的简单.快速的Cache组件.EHCache支持内存和磁盘的 ...

  10. [python] 使用scikit-learn工具计算文本TF-IDF值

    在文本聚类.文本分类或者比较两个文档相似程度过程中,可能会涉及到TF-IDF值的计算.这里主要讲述基于Python的机器学习模块和开源工具:scikit-learn.        希望文章对你有所帮 ...