mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
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:
@ -32,6 +32,7 @@ func (h *minHeap) Top() any {
|
||||
|
||||
/* 基于堆查找数组中最大的 k 个元素 */
|
||||
func topKHeap(nums []int, k int) *minHeap {
|
||||
// 初始化小顶堆
|
||||
h := &minHeap{}
|
||||
heap.Init(h)
|
||||
// 将数组的前 k 个元素入堆
|
||||
|
||||
Reference in New Issue
Block a user