> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/heap-and-priority-queue/high-five.md).

# High Five

{% embed url="<https://www.lintcode.com/problem/613/>" %}

## Solution 1 (Java)

```java
/**
 * Definition for a Record
 * class Record {
 *     public int id, score;
 *     public Record(int id, int score){
 *         this.id = id;
 *         this.score = score;
 *     }
 * }
 */
public class Solution {
    /**
     * @param results a list of <student_id, score>
     * @return find the average of 5 highest scores for each person
     * Map<Integer, Double> (student_id, average_score)
     */
    public Map<Integer, Double> highFive(Record[] results) {
        Map<Integer, Double> ans = new HashMap<>();
        Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();
        for (Record r : results) {
            map.putIfAbsent(r.id, new PriorityQueue<Integer>());
            PriorityQueue<Integer> pq = map.get(r.id);
            if (pq.size() < 5) {
                pq.offer(r.score);
            } else {
                if (r.score > pq.peek()) {
                    pq.poll();
                    pq.offer(r.score);
                }
            }
        }

        for (Map.Entry<Integer, PriorityQueue<Integer>> e : map.entrySet()) {
            int id = e.getKey();
            PriorityQueue<Integer> scores = e.getValue();
            double avg = 0;
            for (Integer s : scores) {
                avg += s;
            }
            avg /= 5.0;
            ans.put(id, avg);
        }

        return ans;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.yushunchen.com/algo/heap-and-priority-queue/high-five.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
