当前位置:网站首页>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
边栏推荐
- uboot通过终端发送‘r‘字符读取ddr内存大小
- After reading who moved my cheese
- Using lazy < t > in C # to realize singleton mode in WPF
- OSPF - authentication and load balancing summary (including configuration commands)
- Uboot reads the DDR memory size by sending 'R' characters through the terminal
- Vfpbs uploads excel and saves MSSQL to the database
- [chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
- Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion
- Xi'an Jiaotong 21st autumn economics online homework answer sheet (III) [standard answer]
- Use the code cloud publicholiday project to determine whether a day is a working day
猜你喜欢

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

token 过期后,如何自动续期?

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

1380. lucky numbers in matrices

Solidity - Security - reentrancy attack

After getting these performance test decomposition operations, your test path will be more smooth

The minecraft server address cannot be refreshed.

Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?

86. separate linked list
![[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]](/img/f3/e51cb80f838faba8dfd90596d6e57b.jpg)
[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
随机推荐
剑指 Offer 18. 删除链表的节点
Basic operations of C language
UE4_ Editor UMG close window cannot destroy UMG immediately
Use of OpenCL thread algebra library viennacl
炒股用指南针开户交易安全吗?
Promise知识点拾遗
Here comes the nearest chance to Ali
[typescript] cannot redeclare block range variables
雲服務器部署 Web 項目
El table lazy load refresh
Fifty years ago, the go code first submitted by the inventor of Hello world was as long as this
What kind of answer has Inspur given in the big AI model landing test?
云服务器部署 Web 项目
Rotating box target detection mmrotate v0.3.1 getting started
2021-10-31
旋转框目标检测mmrotate v0.3.1 训练DOTA数据集(二)
Detailed explanation of issues related to SSL certificate renewal
Xctf--Web--Challenge--area Wp
Attempt to redefine 'timeout' at line 2 solution
09- [istio] istio service entry