注意第K大,要用小顶堆。因为get取走的都是小的,留下的k个都是最大的。

from queue import PriorityQueue

class Solution:
    def findKthLargest(self, nums: List[int], k: int) -> int:
        pq = PriorityQueue()
        for num in nums:
            pq.put(num) # smaller first
            if pq.qsize() > k:
                pq.get()
                
        result = pq.get()
        return result

  

相关文章:

  • 2021-05-04
  • 2021-04-29
  • 2021-10-09
  • 2021-08-03
  • 2021-07-27
  • 2021-09-12
猜你喜欢
  • 2021-12-19
  • 2022-01-15
  • 2022-01-17
  • 2021-09-30
  • 2022-12-23
相关资源
相似解决方案