Code Logo

Top Three Scores with PriorityQueue

Published at22 Apr 2026
Collections Framework Medium 0 views
Like0

A training center records many quiz scores during the week, but the instructor only wants to display the top three scores on a notice board. The full list may be longer than needed, and the board should end up with just the highest three values.

Your task is to return a list containing the three highest scores in descending order. If there are fewer than three scores, return all of them, still in descending order.

For example, if the input is [70, 88, 91, 65, 95, 84], the result should be [95, 91, 88]. If the input is [40, 50], the result should be [50, 40].

This problem is a good medium-level Collections Framework exercise because it is not just about storing values. You need a collection that helps you keep the best few items while scanning the input.

A PriorityQueue is useful here because it can hold the current top candidates and let you remove the smallest one whenever the group grows beyond three.

Example Input & Output

Example 1
Input
scores = [40, 50]
Output
[50, 40]
Explanation

If there are fewer than three values, return all of them in descending order.

Example 2
Input
scores = [90, 90, 85, 72]
Output
[90, 90, 85]
Explanation

Equal high scores can both stay in the result.

Example 3
Input
scores = [70, 88, 91, 65, 95, 84]
Output
[95, 91, 88]
Explanation

Only the three highest scores remain, in descending order.

Algorithm Flow

Recommendation Algorithm Flow for Top Three Scores with PriorityQueue
Recommendation Algorithm Flow for Top Three Scores with PriorityQueue

Solution Approach

This problem becomes much cleaner once you realize that you never need to keep the whole list sorted at all times. You only care about the best three scores seen so far. That is exactly the kind of situation where a PriorityQueue helps.

In Java, the default PriorityQueue acts like a min-heap. That means the smallest value in the queue is easy to inspect and remove. So the strategy is to keep a min-heap whose size never grows beyond three.

As you scan each score, add it to the queue. If the queue size becomes four, remove one item. Because it is a min-heap, the removed item will be the smallest among the four, leaving only the three highest scores seen so far.

The heart of the algorithm looks like this:

PriorityQueue<Integer> heap = new PriorityQueue<>();
for (int score : scores) {
    heap.offer(score);
    if (heap.size() > 3) {
        heap.poll();
    }
}

After that loop, the queue contains the answer values, but not yet in the final descending format. Since polling from a min-heap gives the smallest remaining value first, you can build a list from the queue and then reverse it, or insert values at the front as you remove them.

This approach is more efficient than sorting the entire list when the requirement is only to keep the top few values. The full runtime is O(n log 3), which is effectively O(n) here because the heap never gets large. The important lesson is choosing a collection based on the exact job you need it to do.

Best Answers

java - Approach 1
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;

class Solution {
    public static List<Integer> topThreeScores(List<Integer> scores) {
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        for (int score : scores) {
            heap.offer(score);
            if (heap.size() > 3) {
                heap.poll();
            }
        }
        List<Integer> result = new ArrayList<>(heap);
        result.sort(Collections.reverseOrder());
        return result;
    }
}