当前位置:网站首页>Related applications of priority queue
Related applications of priority queue
2022-06-30 05:46:00 【, ChuChu】
One 、topK problem
Problem description : from N Before the number is found K A big number
such as {1,2,3,4,5} Middle front 2 The big one is 4 and 5
Here is how to use priority queues , The idea is :
- Use the first in the array K The first element builds a small root heap
- Then traverse the array values , If this value is greater than the heap top element, the heap top element will be ejected
- Then put this value into a small root heap
Because the smallest value in the heap is ejected every time , So the last thing left K The value must be the largest .
In this way , It's not hard for us to get :
- Seek before K The biggest , Build small root pile
- Seek before K Minimum , Build a big pile
- Please K Big , Is the top element of the adjusted small root heap ( The top element must be this K The smallest of the three )
- Please K Small , Is the adjusted top element of the large root heap
Here we use code to write a pre K It's the smallest value
// Before array K Small value
public static int[] topK(int[] array, int k){
// Create a large root heap
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(k, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
for (int i = 0; i < array.length; i++){
// hold K Data into the heap
if (priorityQueue.size() < k){
priorityQueue.offer(array[i]);
}else {
// Start comparing
int top = priorityQueue.peek();
// Array elements are put in when they are small
if (top > array[i]){
priorityQueue.poll();
priorityQueue.offer(array[i]);
}
}
}
int[] ans = new int[k];
for (int i = 0; i < k; i++){
ans[i] = priorityQueue.poll();
}
return ans;
}Time complexity :O(N*logK)
An exercise :373. Look for the smallest K Pairs of numbers
Two 、 Heap sort
Problem description : hold N Sort the number from small to large
From small to large , Adopt large root pile , step :
- 1、 Build the entire array into a large root heap
- 2、 Swap the top and tail elements of the heap , And then from 0 Start adjusting the large root heap
- 3、 Use one end Remember tail elements , Again --, Make sure the largest element is at the bottom
public void heapSort(){
int end = usedSize - 1;
while (end > 0){
int tmp = elem[end];
elem[end] = elem[0];
elem[0] = tmp;
shiftDown(0, end);
end--;
}
}
Thank you for seeing this
边栏推荐
- Acwing winter vacation daily question 2022 punch in day 11
- English grammar_ Adjective / adverb Level 3 - superlative
- Sword finger offer 18 Delete the node of the linked list
- /Path/to/ idiom, not a command
- 3D rotation album
- [typescript] experimentaldecorators of vscode stepping pit
- PyGame. Why can't I exit when I click X in the window? I can only exit when I return idle
- 旋转框目标检测mmrotate v0.3.1入门
- Wechat applet training 2
- 10-【istio】istio Sidecar
猜你喜欢

剑指 Offer 22. 链表中倒数第k个节点

Assembly learning tutorial: accessing memory (3)

【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)

Visualization of 3D geological model based on borehole data by map flapping software

Responding with flow layout

强烈推荐十几款IDEA开发必备的插件

Who is promoting the new inflection point of audio and video industry in 2022?

【板栗糖GIS】global mapper—如何把栅格的高程值赋予给点

What kind of answer has Inspur given in the big AI model landing test?

Solidy - fallback function - 2 trigger execution modes
随机推荐
Question mark (?) in Cron expression Use of
[Blue Bridge Road -- bug free code] analysis of AT24C02 storage code
Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation
Rotating frame target detection mmrotate v0.3.1 learning configuration
Sword finger offer 29 Print matrix clockwise
How to automatically renew a token after it expires?
Database SQL language 04 subquery and grouping function
inno setup 最简单的自定义界面效果
8 ways to earn passive income
On line assignment of financial cost management in the 22nd spring of Western Polytechnic University [Full Score answer]
OpenCL线程代数库ViennaCL的使用
We strongly recommend more than a dozen necessary plug-ins for idea development
Qt之QListView的简单使用(含源码+注释)
Database SQL language 06 single line function
二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
图扑软件基于钻孔数据的三维地质模型可视化
OSPF - authentication and load balancing summary (including configuration commands)
Delete the repeating elements in the sorting list (simple questions)
Answer sheet for online assignment of "motor and drive" of Xijiao 21 autumn (IV) [standard answer]
Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (II)