当前位置:网站首页>Leetcode - the k-th element in 703 data flow (design priority queue)
Leetcode - the k-th element in 703 data flow (design priority queue)
2022-07-03 10:12:00 【Cute at the age of three @d】


Priority queue

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); */
边栏推荐
- 20220609其他:多数元素
- pycharm 无法引入自定义包
- [C question set] of Ⅵ
- Retinaface: single stage dense face localization in the wild
- CV learning notes - feature extraction
- 4G module board level control interface designed by charging pile
- Opencv notes 20 PCA
- Qcombox style settings
- QT is a method of batch modifying the style of a certain type of control after naming the control
- Leetcode-513:找树的左下角值
猜你喜欢

Leetcode 300 最长上升子序列

Opencv+dlib to change the face of Mona Lisa

Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction

LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))

Opencv image rotation

CV learning notes - edge extraction

03 fastjason solves circular references

2.1 Dynamic programming and case study: Jack‘s car rental

LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)

Matplotlib drawing
随机推荐
4G module board level control interface designed by charging pile
Leetcode-100:相同的树
Problems encountered when MySQL saves CSV files
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
LeetCode - 705 设计哈希集合(设计)
2021-11-11 standard thread library
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
Opencv Harris corner detection
2312. Selling wood blocks | things about the interviewer and crazy Zhang San (leetcode, with mind map + all solutions)
20220531 Mathematics: Happy numbers
One click generate traffic password (exaggerated advertisement title)
Opencv gray histogram, histogram specification
Toolbutton property settings
CV learning notes - edge extraction
openCV+dlib实现给蒙娜丽莎换脸
LeetCode - 919. Full binary tree inserter (array)
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
Leetcode-513: find the lower left corner value of the tree
Opencv image rotation
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)