当前位置:网站首页>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
边栏推荐
- inno setup 最简单的自定义界面效果
- Video summary of my station B
- After getting these performance test decomposition operations, your test path will be more smooth
- Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
- 动态规划--怪盗基德的滑翔翼
- C语言基础小操作
- Codeforces B. MEX and Array
- 企事业单位源代码防泄露工作该如何进行
- Introduction to mmcv common APIs
- Xi'an Jiaotong 21st autumn economics online homework answer sheet (III) [standard answer]
猜你喜欢

How to automatically renew a token after it expires?

Transfer the token on the matic-erc20 network to the matic polygon

旋转框目标检测mmrotate v0.3.1入门

UE4_ Editor UMG close window cannot destroy UMG immediately

How to create a CSR (certificate signing request) file?

Xctf attack and defense world crypto advanced area

Assembly learning tutorial: accessing memory (3)

剑指 Offer 18. 删除链表的节点

Codeforces B. MEX and Array

企事业单位源代码防泄露工作该如何进行
随机推荐
Baiwen.com 7 days Internet of things smart home learning experience punch in the third day
What do you think of the deleted chat records? How to restore the deleted chat records on wechat?
[Blue Bridge Road -- bug free code] analysis of AT24C02 storage code
Sword finger offer 18 Delete the node of the linked list
Leader: who can use redis expired monitoring to close orders and get out of here!
【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)
OpenCL线程代数库ViennaCL的使用
I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?
Promise knowledge points
token 过期后,如何自动续期?
Transfer the token on the matic-erc20 network to the matic polygon
ECS deployment web project
At the beginning of 2022, people who are ready to change jobs should pay attention to
Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
Did you know that WPS can turn on eye protection mode?
hashlips_ art_ Engine-1.0.6 usage
The minecraft server address cannot be refreshed.
What kind of answer has Inspur given in the big AI model landing test?
Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion
Intelligent deodorizer embedded development