feat: add top_k.c and refactor top_k.js (#889)

* Add top_k.c based on my_heap.c

* Improve the implementation of top_k.js

* Add a comment to top_k
This commit is contained in:
Yudong Jin
2023-10-26 02:54:19 +08:00
committed by GitHub
parent 9f4076d1c1
commit 7822bf9cd4
13 changed files with 190 additions and 73 deletions

View File

@@ -8,7 +8,7 @@ import '../utils/print_util.dart';
/* 基于堆查找数组中最大的 k 个元素 */
MinHeap topKHeap(List<int> nums, int k) {
// 将数组的前 k 个元素入堆
// 初始化小顶堆,将数组的前 k 个元素入堆
MinHeap heap = MinHeap(nums.sublist(0, k));
// 从第 k+1 个元素开始,保持堆的长度为 k
for (int i = k; i < nums.length; i++) {