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]
time, space.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
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]
time, space.