mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
fine tune
This commit is contained in:
@ -8,7 +8,7 @@ const { MaxHeap } = require('./my_heap');
|
||||
|
||||
/* 基于堆查找数组中最大的 k 个元素 */
|
||||
function topKHeap(nums, k) {
|
||||
// 使用大顶堆 MaxHeap,对数组 nums 取相反数
|
||||
// 使用大顶堆 MaxHeap ,对数组 nums 取相反数
|
||||
const invertedNums = nums.map((num) => -num);
|
||||
// 将数组的前 k 个元素入堆
|
||||
const heap = new MaxHeap(invertedNums.slice(0, k));
|
||||
|
||||
Reference in New Issue
Block a user