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大的数,优先队列,小顶堆。

class KthLargest {

    private PriorityQueue<Integer> q;
private int k; public KthLargest(int k, int[] nums) {
this.k = k;
q = new PriorityQueue<>(k);
for (int n : nums)
add(n);
} public int add(int val) {
if (q.size() < k)
q.offer(val);
else if (q.peek() < val) {
q.poll();
q.offer(val);
}
return q.peek();
}
}

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

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

  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. WordPress主题开发:footer.php

    最简 <?php wp_footer();?> </body> </html>

  2. 06、action操作开发实战

    1.reduce: 2.collect: 3.count: 4.take: 5.saveAsTextFile: 6.countByKey: 7.foreach: package sparkcore.j ...

  3. ethtool 解决网卡丢包严重和网卡原理

      1 概述 最近业务上老有问题,查看发现overruns值不断增加,学习了一下相关的知识.发现数值也在不停的增加.发现这些 errors, dropped, overruns 表示的含义还不大一样. ...

  4. OUTLOOK - Unable to Delete Meetings

    Error Information: Resolved: Check Meeting room permission.Get-MailboxFolderPermission MeetingRoom_1 ...

  5. float 浮点数与零值0比较大小 ZZ

    float x: 千万不要写x==0; 写出float x 与“零值”比较的if语句——一道面试题分析 写出float  x 与“零值”比较的if语句 请写出 float  x 与“零值”比较的 if ...

  6. C# 程序打包成安装项目

    Xaml设置图标https://blog.csdn.net/u014234260/article/details/73648649 winform程序控制面板中卸载显示图标https://blog.c ...

  7. openssl RSA密钥格式PKCS1和PKCS8相互转换

    RSA私钥格式PKCS1和PKCS8相互转换 RSA公钥格式PKCS1和PKCS8相互转换 以下转换基于openssl命令的操作: 1. openssl 生成pkcs1格式的私钥,密钥长度1024位, ...

  8. MySQL出现Access denied for user ‘root’@’localhost’ (using password:YES)

    连接时MySQL出现了下面的错误: Access denied for user ‘root’@’localhost’ (using password:YES) 解决的办法是先停止MySQL服务,在m ...

  9. 【C++】C++中的分离式编译

    在C++中随着程序越来越复杂,我们希望把程序的各个部分分别储存在不同的文件中.C++支持的分离式编译(separate compilation)允许我们把程序分割到几个文件中去,每个文件独立编译. 头 ...

  10. LeetCode 232:Implement Queue using Stacks

     Implement the following operations of a queue using stacks. push(x) -- Push element x to the back ...