2023-06-14 18:49:42 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: top_k.cs
|
|
|
|
|
* Created Time: 2023-06-14
|
|
|
|
|
* Author: hpstory (hpstory1024@163.com)
|
|
|
|
|
*/
|
|
|
|
|
|
2023-09-02 23:54:38 +08:00
|
|
|
|
namespace hello_algo.chapter_heap;
|
2023-06-14 18:49:42 +08:00
|
|
|
|
|
|
|
|
|
public class top_k {
|
|
|
|
|
/* 基于堆查找数组中最大的 k 个元素 */
|
2023-11-26 23:18:44 +08:00
|
|
|
|
PriorityQueue<int, int> TopKHeap(int[] nums, int k) {
|
2023-10-26 02:54:19 +08:00
|
|
|
|
// 初始化小顶堆
|
2023-10-08 01:33:46 +08:00
|
|
|
|
PriorityQueue<int, int> heap = new();
|
2023-06-14 18:49:42 +08:00
|
|
|
|
// 将数组的前 k 个元素入堆
|
|
|
|
|
for (int i = 0; i < k; i++) {
|
|
|
|
|
heap.Enqueue(nums[i], nums[i]);
|
|
|
|
|
}
|
|
|
|
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
|
|
|
|
for (int i = k; i < nums.Length; i++) {
|
|
|
|
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
|
|
|
|
if (nums[i] > heap.Peek()) {
|
|
|
|
|
heap.Dequeue();
|
|
|
|
|
heap.Enqueue(nums[i], nums[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return heap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Test() {
|
2023-11-26 23:18:44 +08:00
|
|
|
|
int[] nums = [1, 7, 6, 3, 2];
|
2023-06-14 18:49:42 +08:00
|
|
|
|
int k = 3;
|
2023-10-08 01:33:46 +08:00
|
|
|
|
PriorityQueue<int, int> res = TopKHeap(nums, k);
|
2023-06-14 18:49:42 +08:00
|
|
|
|
Console.WriteLine("最大的 " + k + " 个元素为");
|
|
|
|
|
PrintUtil.PrintHeap(res);
|
|
|
|
|
}
|
|
|
|
|
}
|