The algorithm: Create a min-heap For each element in the array: - Add it to the heap - If heap size exceeds , remove the minimum Return the heap top (the kth largest) Why min-heap? Because we want to discard the smallest elements. The heap top is the smallest of the largest, which is exactly the kth largest.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/heaps-priority-queues/kth-largest-element-solution
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████████████████████████████████████████████████████████████████████████████████████████████████