当前位置:网站首页>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); */
边栏推荐
- Tensorflow built-in evaluation
- CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
- LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
- 4.1 Temporal Differential of one step
- openCV+dlib实现给蒙娜丽莎换脸
- Opencv Harris corner detection
- CV learning notes - deep learning
- Leetcode-112: path sum
- Leetcode-513: find the lower left corner value of the tree
- Tensorflow2.0 save model
猜你喜欢

CV learning notes - feature extraction

Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction

4.1 Temporal Differential of one step

CV learning notes ransca & image similarity comparison hash

LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)

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

Installation and removal of MySQL under Windows

LeetCode - 919. Full binary tree inserter (array)

yocto 技術分享第四期:自定義增加軟件包支持

Swing transformer details-1
随机推荐
LeetCode - 919. 完全二叉树插入器 (数组)
Adaptiveavgpool1d internal implementation
Leetcode-106:根据中后序遍历序列构造二叉树
One click generate traffic password (exaggerated advertisement title)
Matplotlib drawing
LeetCode - 715. Range 模块(TreeSet) *****
20220605 Mathematics: divide two numbers
4G module IMEI of charging pile design
3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
CV learning notes ransca & image similarity comparison hash
2.1 Dynamic programming and case study: Jack‘s car rental
About windows and layout
4.1 Temporal Differential of one step
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
My notes on intelligent charging pile development (II): overview of system hardware circuit design
Swing transformer details-2
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
My 4G smart charging pile gateway design and development related articles
El table X-axis direction (horizontal) scroll bar slides to the right by default
The data read by pandas is saved to the MySQL database