2023-08-08 11:36:45 +0000 UTC

Kth Largest Element in an Array

Code

class Solution:
    def findKthLargest(self, nums, k):
        heap = []
        for num in nums:
            heapq.heappush(heap, num)
            if len(heap) > k:
                heapq.heappop(heap)
        
        return heap[0]