当前位置:网站首页>LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
2022-07-03 09:20:00 【三岁就很萌@D】


优先队列

class KthLargest {
private PriorityQueue<Integer> queue;
private int k;
public KthLargest(int k, int[] nums) {
queue = new PriorityQueue<Integer>();
this.k = k;
for(int num : nums){
if(queue.size() < k){
queue.offer(num);
}
else{
if(num > queue.peek()){
queue.poll();
queue.offer(num);
}
}
}
}
public int add(int val) {
if(queue.size() < k){
queue.offer(val);
}
else{
if(val > queue.peek()){
queue.poll();
queue.offer(val);
}
}
return queue.peek();
}
}
/** * Your KthLargest object will be instantiated and called as such: * KthLargest obj = new KthLargest(k, nums); * int param_1 = obj.add(val); */
边栏推荐
- Stm32 NVIC interrupt priority management
- 没有多少人能够最终把自己的兴趣带到大学毕业上
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- 51 MCU tmod and timer configuration
- Emballage automatique et déballage compris? Quel est le principe?
- Positive and negative sample division and architecture understanding in image classification and target detection
- Adaptiveavgpool1d internal implementation
- How does the memory database give full play to the advantages of memory?
- (2)接口中新增的方法
- On the problem of reference assignment to reference
猜你喜欢

For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer

Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)

My notes on intelligent charging pile development (II): overview of system hardware circuit design

要选择那种语言为单片机编写程序呢

Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from

使用密钥对的形式连接阿里云服务器

Timer and counter of 51 single chip microcomputer

Swing transformer details-2

It is difficult to quantify the extent to which a single-chip computer can find a job

In third tier cities and counties, it is difficult to get 10K after graduation
随机推荐
Notes on C language learning of migrant workers majoring in electronic information engineering
01仿B站项目业务架构
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
Mysql database underlying foundation column
Stm32 NVIC interrupt priority management
Application of 51 single chip microcomputer timer
Simulate mouse click
4G module designed by charging pile obtains signal strength and quality
CEF download, compile project
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Application of external interrupts
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
(1) What is a lambda expression
Positive and negative sample division and architecture understanding in image classification and target detection
YOLO_ V1 summary
Yocto technology sharing phase IV: customize and add software package support
Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
使用sed替换文件夹下文件
(1) 什么是Lambda表达式
Getting started with JMX, MBean, mxbean, mbeanserver