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 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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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+ ...
- 【LeetCode】703. Kth Largest Element in a Stream 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- 【IAP支付之三】苹果IAP安全支付与防范 receipt收据验证
这里网上的朋友已经介绍的很详细了,具体的链接已经无法找到了. 这里主要说几点本人在开发中遇到的问题: 1.漏单必须要处理,玩家花RMB购买的东西却丢失了,是绝对不能容忍的.所谓的漏单就是玩家已经正常付 ...
- 如何快速成为一名Linux运维工程师
如今的互联网,绝大多数的网站.服务.游戏均是跑在Linux上面的,虽说Linux发行版众多,只要玩熟了一种发行版,了解了Linux精髓.基本架构.设计原理,其他都是触类旁通的,千万不要在选择哪一发行版 ...
- Spring注入,Ioc的具体配置
Spring框架的IOC注入: 一.Java部分代码: Person实体类: package com.ioc; import java.util.List; import java.util.Map; ...
- CSS(一)属性--border边框
HTML代码 <body> <div>举个例子</div> </body> CSS代码: div{ font-size:12px; //字体大小,默认 ...
- 7.5 C++基本序列式容器
参考:http://www.weixueyuan.net/view/6402.html 总结: vector可以理解为可以在两端插入.删除数据的数组,它提供了丰富的成员函数,用于操作数据. begin ...
- jdbc中Class.forName(driverName)的作用
上次面试别人问我jdbc的过程: 我是这样回答的: Class.forName加载驱动 DriverManager.connect(url,username, password)获取连接对象 conn ...
- 强化学习4-时序差分TD
之前讲到强化学习在不基于模型时可以用蒙特卡罗方法求解,但是蒙特卡罗方法需要在每次采样时生产完整序列,而在现实中,我们很可能无法生成完整序列,那么又该如何解决这类强化学习问题呢? 由贝尔曼方程 vπ(s ...
- 《RECURRENT BATCH NORMALIZATION》
原文链接 https://arxiv.org/pdf/1603.09025.pdf Covariate 协变量:在实验的设计中,协变量是一个独立变量(解释变量),不为实验者所操纵,但仍影响实验结果. ...
- day 关于生成器的函数
def average(): count=0 toatal=0 average=0 while True: value=yield average toatal+=value count+=1 ave ...
- FreeSWITCH视频会议命令
列出所有会议 conference list 列出会议的所有成员 conference <conference_name> list 而<conference_name>就是会 ...