Files
hello-algo/ja/codes/csharp/chapter_heap/top_k.cs
T
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

38 lines
1.2 KiB
C#

/**
* File: top_k.cs
* Created Time: 2023-06-14
* Author: hpstory ([email protected])
*/
namespace hello_algo.chapter_heap;
public class top_k {
/* ヒープを使って配列内の大きいほうから k 個の要素を求める */
PriorityQueue<int, int> TopKHeap(int[] nums, int k) {
// 最小ヒープを初期化する
PriorityQueue<int, int> heap = new();
// 配列の先頭 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() {
int[] nums = [1, 7, 6, 3, 2];
int k = 3;
PriorityQueue<int, int> res = TopKHeap(nums, k);
Console.WriteLine("大きいほうから " + k + " 個の要素は");
PrintUtil.PrintHeap(res);
}
}