I did this question using heapsort, then why is it giving memory limit exceeded?


#1

i did this question using heapsort, then why is it giving memory limit exceeded ?? what’s the better solution


#2

Instead of heapsort, use a max heap that maintains K elements and whenever there is a number that is less than the top of the heap, remove the top and insert that element, after looping through the entire list you will have the answer at the top of the heap.


#3

I have used a max heap(priority_queue) yet it is throwing a MLE.