当前位置:网站首页>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); */
边栏推荐
- (2) New methods in the interface
- One click generate traffic password (exaggerated advertisement title)
- 20220602 Mathematics: Excel table column serial number
- Tensorflow built-in evaluation
- Dynamic layout management
- 20220604数学:x的平方根
- 2021-11-11 standard thread library
- 使用密钥对的形式连接阿里云服务器
- 20220606数学:分数到小数
- 3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
猜你喜欢
随机推荐
CV learning notes - feature extraction
openCV+dlib实现给蒙娜丽莎换脸
Tensorflow2.0 save model
CV learning notes - scale invariant feature transformation (SIFT)
Leetcode-106:根据中后序遍历序列构造二叉树
. DLL and Differences between lib files
Google browser plug-in recommendation
Swing transformer details-1
MySQL root user needs sudo login
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
What can I do to exit the current operation and confirm it twice?
4G module board level control interface designed by charging pile
About windows and layout
20220608其他:逆波兰表达式求值
Window maximum and minimum settings
Leetcode-404: sum of left leaves
LeetCode - 933 最近的请求次数
4G module at command communication package interface designed by charging pile
Installation and removal of MySQL under Windows
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software