2023-08-17 05:00:35 +08:00
|
|
|
/**
|
|
|
|
* File: top_k.js
|
|
|
|
* Created Time: 2023-08-13
|
|
|
|
* Author: Justin (xiefahit@gmail.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
const { MaxHeap } = require('./my_heap');
|
|
|
|
|
|
|
|
/* 基于堆查找数组中最大的 k 个元素 */
|
2023-09-10 23:11:50 +08:00
|
|
|
function topKHeap(nums, k) {
|
2023-09-17 01:13:15 +08:00
|
|
|
// 使用大顶堆 MaxHeap ,对数组 nums 取相反数
|
2023-09-02 23:07:47 +08:00
|
|
|
const invertedNums = nums.map((num) => -num);
|
2023-08-17 05:00:35 +08:00
|
|
|
// 将数组的前 k 个元素入堆
|
|
|
|
const heap = new MaxHeap(invertedNums.slice(0, k));
|
|
|
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
|
|
|
for (let i = k; i < invertedNums.length; i++) {
|
|
|
|
// 若当前元素小于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
|
|
|
if (invertedNums[i] < heap.peek()) {
|
|
|
|
heap.pop();
|
|
|
|
heap.push(invertedNums[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 取出堆中元素
|
|
|
|
const maxHeap = heap.getMaxHeap();
|
|
|
|
// 对堆中元素取相反数
|
2023-09-02 23:07:47 +08:00
|
|
|
const invertedMaxHeap = maxHeap.map((num) => -num);
|
2023-08-17 05:00:35 +08:00
|
|
|
return invertedMaxHeap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
const nums = [1, 7, 6, 3, 2];
|
|
|
|
const k = 3;
|
2023-09-10 23:11:50 +08:00
|
|
|
const res = topKHeap(nums, k);
|
2023-08-17 05:00:35 +08:00
|
|
|
console.log(`最大的 ${k} 个元素为`, res);
|