Say we have a Priority Queue of size 1000 that is implemented using Max Heap.
Now if i want to get the top 5 elements, the most laid back method is to poll the maximum 5 times, resulting in a set of five maximum numbers. The complexity of a poll function using heap is Log N. Hence, retrieving 5 top values or say, K top values will result in complexity KLogN.
Is there any other way of getting the top K values using priority queue with lesser complexity?
The solution Find k maximum numbers from a heap of size n in O(klog(k)) time seems to increase the complexity rather than decreasing it, which is why i am trying to think about some other logic. there is a sort function in the logic which will itself consume kLogk for finding one. hence say, for 5 items..it will be k*(kLogK) if K=5? hence, its not decreasing complexity.