Solution Review: Finding “K” Largest Elements in the Array
Learn how to find “k” largest elements in the array challenge.
We'll cover the following...
We'll cover the following...
Solution: Creating a max-heap and removing max “k” times
Here create a heap, and then call the buildHeap function (lines 14-15) to create a maximum heap from the given array. To find the k largest elements in an array:
-
You get the maximum value from the heap.
-
Save the result to the vector
output. -
Remove the maximum value from the heap.
You will repeat the same steps k times (provided k < size).
Time complexity
The time complexity of building a heap is ...