Solution Review: Finding “K” Smallest Elements in the Array
Look over the solution review of the challenge given in the previous lesson.
We'll cover the following...
We'll cover the following...
Solution 1: removeMin() times
Here create a heap and then call the buildHeap (line 12) function to create a minimum heap from the given array. To find the k smallest elements in an array:
-
You get the minimum value from the heap.
-
Save the result to the vector
output. -
Remove the minimum value from the heap.
Repeat the same steps k times (provided k < size).
Time complexity
The time complexity of building a heap is ...