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.

遍历数组时将数字加入优先队列(堆),一旦堆的大小大于k就将堆顶元素去除,确保堆的大小为k。遍历完后堆顶就是返回值。

class KthLargest {
PriorityQueue <Integer> pq;
int size;
public KthLargest(int k, int[] nums) {
this.pq = new PriorityQueue<>();
this.size = k;
for(int num : nums){
add(num);
}
} public int add(int val) {
pq.offer(val);
if(pq.size() > this.size){
pq.poll();
}
return pq.peek();
}
} /**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/

  

LeetCode - Kth Largest Element in a Stream的更多相关文章

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

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

  3. Python3解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 el ...

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

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

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

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

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

  8. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

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

随机推荐

  1. UVa LA 3695 - Distant Galaxy 前缀和,状态拆分,动态规划 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. sqlalchem表关联(一对多,一对一,多对多)

    简介: 一:一对多关系 1.表示一对多的关系时,在子表类中通过 foreign key (外键)限制本列的值,然后,在父表类中通过 relationship() 方法来引用子表的类. 2.示例代码: ...

  3. 福大软工1816 · 第三次作业 - 结对项目Salty Fish原型图

    SALTY FISH原型图 LINKS IMPORT to LIST FOCUS TRENDS ANALYSE NIGHT

  4. border_mode

    如果border_mode选择为same,那么卷积操作的输入和输出尺寸会保持一致.如果选择valid,那卷积过后,尺寸会变小 # apply a 3x3 convolution with 64 out ...

  5. SQL-14 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。 注意对于重复的emp_no进行忽略。

    题目描述 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t.注意对于重复的emp_no进行忽略.CREATE TABLE IF NOT EXISTS &q ...

  6. 在linux执行kettle

    1.下载 最新版下载 7.1 https://community.hitachivantara.com/docs/DOC-1009855 2.准备 3.上传任务文件 .kjb,.ktr 4.上传mys ...

  7. cnn 经典网络结构 解析

    cnn发展史 这是imageNet比赛的历史成绩 可以看到准确率越来越高,网络越来越深. 加深网络比加宽网络有效的多,这已是公认的结论. cnn结构演化图 AlexNet 诞生于2012年,因为当时用 ...

  8. linux程序的常用保护机制

    操作系统提供了许多安全机制来尝试降低或阻止缓冲区溢出攻击带来的安全风险,包括DEP.ASLR等.在编写漏洞利用代码的时候,需要特别注意目标进程是否开启了DEP(Linux下对应NX).ASLR(Lin ...

  9. leetcode31题:下一个排列

    实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数空间. ...

  10. day 36 关于io模型的问题 阻塞 和多路复用

    # from gevent import spawn,monkey;monkey.patch_all()# from socket import *# def server(ip,port):# se ...