当前位置:网站首页>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); */
边栏推荐
- Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
- 01 business structure of imitation station B project
- 当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
- 没有多少人能够最终把自己的兴趣带到大学毕业上
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- After clicking the Save button, you can only click it once
- 对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
- Leetcode 300 最长上升子序列
- STM32 interrupt switch
- A lottery like scissors, stone and cloth (C language)
猜你喜欢
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
Timer and counter of 51 single chip microcomputer
学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里
Yocto Technology Sharing Phase 4: Custom add package support
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
51 MCU tmod and timer configuration
要選擇那種語言為單片機編寫程序呢
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
随机推荐
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
Serial port programming
4G module designed by charging pile obtains signal strength and quality
yocto 技术分享第四期:自定义增加软件包支持
Positive and negative sample division and architecture understanding in image classification and target detection
Screen display of charging pile design -- led driver ta6932
使用密钥对的形式连接阿里云服务器
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
4G module IMEI of charging pile design
Sending and interrupt receiving of STM32 serial port
嵌入式系统没有特别明确的定义
When the reference is assigned to auto
01仿B站项目业务架构
Which language should I choose to program for single chip microcomputer
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
自动装箱与拆箱了解吗?原理是什么?
Circular queue related design and implementation reference 1
[keil5 debugging] warning:enumerated type mixed with other type
The data read by pandas is saved to the MySQL database