Turn a PriorityQueue Into a Max Heap
Turn a PriorityQueue Into a Max Heap
Complete largest(int[] scores, int k) so it returns the k largest scores from biggest to smallest, as one string with a single space between them.
Reminder: a max heap with PriorityQueue. By default Java's PriorityQueue is a min heap, so poll() always hands back the smallest value. Passing Collections.reverseOrder() to the constructor flips every comparison inside it, and the same queue becomes a max heap: now poll() hands back the largest value first. Nothing else about add, peek and poll changes.
Polling a max heap k times gives you the k biggest values in order, without sorting the whole array yourself.
Requirements:
1. Build the queue as a max heap with Collections.reverseOrder(). The loop that adds every score is already written.
2. Poll at most k scores. If the heap runs out first, stop with what you have, so asking for more scores than exist returns all of them.
3. Separate scores with one space and put no space at the start or the end. A k of 0, or an empty array, gives an empty string.
4. Keep duplicates: two equal scores both count.
Do NOT write a main method.
Expected Output:
[100 95 88] [100] [100 95 88 82 70 64]
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...