LeetCode 215 Kth Largest Element in an Array - Implementation

The approach

Min-heap of size k. After processing all, return heap minimum.

function findKthLargest(nums, k): heap = [] for num in nums: if len(heap) < k: heappush(heap, num) elif num > heap[0]: heapreplace(heap, num) return heap[0]

O(nlogk)O(n \log k) time, O(k)O(k) space.