美文网首页
Leetcode--Heap

Leetcode--Heap

作者: Morphiaaa | 来源:发表于2017-06-08 14:23 被阅读0次

215. Kth Largest Element in an Array

要找第K大的数,就是找第len(nums)-k-1小的数,构造一个最小堆,将前边len(nums)-k小的数都pop出去,堆顶就成了第len(nums)-k-1小的数。
算法复杂度:O(n+(n-k)log(n))
更多解法参考:
https://discuss.leetcode.com/topic/22159/python-different-solutions-with-comments-bubble-sort-selection-sort-heap-sort-and-quick-sort

347. Top K Frequent Elements

use heap sort, we only keep k elements in heap, the k elements are the top k frequent elements
first we count the frequency of each element, then we pick the top frequent elements and put them into heap, we only keep the heap length as k, so it must be the top k frequent elements.

相关文章

  • Leetcode--Heap

    215. Kth Largest Element in an Array 要找第K大的数,就是找第len(nums...

网友评论

      本文标题:Leetcode--Heap

      本文链接:https://www.haomeiwen.com/subject/syzsottx.html