chapter_heap/top_k/ #552
Replies: 26 comments 26 replies
-
第一次知道还有这么玩的,有点意思 |
Beta Was this translation helpful? Give feedback.
-
heapq.heappush(heap, nums[i]),这一步push之后不需要heapify吗 |
Beta Was this translation helpful? Give feedback.
-
建长度为k的小顶堆实现top-K实在是泰裤辣 |
Beta Was this translation helpful? Give feedback.
-
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆 |
Beta Was this translation helpful? Give feedback.
-
建议解释一下为什么前k大的元素需要小根堆而不是大根堆 这里是个大坑 |
Beta Was this translation helpful? Give feedback.
-
太棒了
// 将数组的前 k 个元素入堆
for (int i = 0; i < k; i++) {
heap.offer(nums[i]);
} 可以优化成 Queue<Integer> heap = new PriorityQueue<Integer>(Arrays.stream(nums).collect(Collectors.toList()));
|
Beta Was this translation helpful? Give feedback.
-
遍历和排序提供代码: 遍历Python: def findKthLargest(nums, k):
result = []
for i in range(k):
max_num = max(nums)
result.append(max_num)
nums.remove(max_num)
return result Go: func findKthLargest(nums []int, k int) []int {
result := make([]int, k)
for i := 0; i < k; i++ {
max := nums[0]
index := 0
for i, num := range nums {
if num > max {
max = num
index = i
}
}
result[i] = max
nums = append(nums[:index], nums[index+1:]...)
}
return result
} 排序Python: def findKthLargest(nums, k):
nums.sort(reverse=True)
return nums[:k] Go: func findKthLargest(nums []int, k int) []int {
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
return nums[:k]
}
//或者
func findKthLargest(nums []int, k int) []int {
sort.Slice(nums, func(i, j int) bool {
return nums[i] > nums[j]
})
return nums[:k]
} |
Beta Was this translation helpful? Give feedback.
-
小顶堆是不是相当于会自动排序的数组,会把最小的数排到堆顶?不过如果K较大时,排数的过程会不会很耗时? |
Beta Was this translation helpful? Give feedback.
-
想请教一下,为什么 JS / TS 与 其他编程语言处理方式不同呢? JS / TS 中是采用 : 先对 nums 列表j进行遍历为相反数,再通过 new MaxHeap() .从而实现初始化为 minHeap 小顶堆。 导致判断逻辑也与其他编程语言不同 那 为什么不一开始就使用 new MinHeap(),这样还节省了之前的列表遍历置反,后续又要置反输出最终结果, |
Beta Was this translation helpful? Give feedback.
-
请问代码实现部分的C语言为什么显示的是 |
Beta Was this translation helpful? Give feedback.
-
大佬,想请教一下实现部分的python部分跟“把nums取负转成小顶堆,然后取k次堆顶元素,再取反”相比,哪个速度会更快呢?
|
Beta Was this translation helpful? Give feedback.
-
是不是我理解有问题?如果第k+1个元素很大的话,是不是有问题?比如[100,1,2,3,4,5],查找Top-3时,后面的元素都不会入堆 |
Beta Was this translation helpful? Give feedback.
-
heapq 模块 中也提供了一样的nlargest方法 |
Beta Was this translation helpful? Give feedback.
-
为什么是n轮出堆,不应该是n-k次么 |
Beta Was this translation helpful? Give feedback.
-
注意: 元素入堆 这里应该指的是单个元素入堆的时间复杂度为O(log n),对于将n个元素入堆的总体平均时间复杂度前文也提到为O(nlog n), 只不多通过建堆的操作优化可以将时间复杂度降低到O(n) |
Beta Was this translation helpful? Give feedback.
-
leetcode 原题:215. 数组中的第K个最大元素 |
Beta Was this translation helpful? Give feedback.
-
若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆 |
Beta Was this translation helpful? Give feedback.
-
想询问一下8.3 Top-k问题中方法三为什么C++的函数传入的是个引用,这边对原来的数组没有进行修改,是出于效率问题还是出于习惯传入了一个引用 |
Beta Was this translation helpful? Give feedback.
-
想问下C#的8.3.3示例代码,是忘记大顶堆取反了吗?没有很理解小顶堆怎么构造的 |
Beta Was this translation helpful? Give feedback.
-
总结: |
Beta Was this translation helpful? Give feedback.
-
C語言示例中的 |
Beta Was this translation helpful? Give feedback.
-
NOTE:用 |
Beta Was this translation helpful? Give feedback.
-
将数组的前 k 个元素入堆的时候可以考虑改用自下而上的堆化,时间复杂度为O(log k);之后最多进行 n - k 轮出堆和 n - k 轮入堆,所以时间复杂度最多为O(log k + 2(n - k) · log k) |
Beta Was this translation helpful? Give feedback.
-
top-k完整实现#include using namespace std; vector top_k(vector& nums, int k) { int main() { |
Beta Was this translation helpful? Give feedback.
-
top_k最大k个元素的C语言版本代码 void top_k(float *arr, int size, float *res, int k) {
if (k <= 0 || k > size) return;
float *heap = (float *)malloc(sizeof(float) * k); int heap_size = 0;
for (int i = 0; i < k; i++){ pushHeap(heap, &heap_size, arr[i]); } // // 初始化堆,放入前 k 个元素
for (int i = k; i < size; i++){ if (arr[i] > heap[0]) { float tmp; popHeap(heap, &heap_size, &tmp); pushHeap(heap, &heap_size, arr[i]); } } // 处理剩余的元素
for (int i = 0; i < k; i++) { popHeap(heap, &heap_size, &res[k - 1 - i]); } // 将堆中的元素按降序输出到 res 数组
freeHeap(&heap);
} |
Beta Was this translation helpful? Give feedback.
-
这C写的topk肯定有问题啊,怎么能初始化一个长度为0的heap然后往里push呢,这肯定段错误啊 |
Beta Was this translation helpful? Give feedback.
-
chapter_heap/top_k/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_heap/top_k/
Beta Was this translation helpful? Give feedback.
All reactions