There are two properties in the node student id and scores, to ensure that each student will have at least 5 points, find the average of 5 highest scores for each person.
/** * Definition for a Record * class Record { * public int id, score; * public Record(int id, int score){ * this.id = id; * this.score = score; * } * } */publicclassSolution {publicMap<Integer,Double> highFive(Record[] results) {Map<Integer,Double> ans =newHashMap<>();Map<Integer,PriorityQueue<Integer>> minHeapMap =newHashMap<>();for(Record r: results){if(!minHeapMap.containsKey(r.id)){minHeapMap.put(r.id,newPriorityQueue<>(5)); }minHeapMap.get(r.id).add(r.score);if(minHeapMap.get(r.id).size() >5){minHeapMap.get(r.id).poll(); } }for(Map.Entry<Integer,PriorityQueue<Integer>> entry:minHeapMap.entrySet()){double sum =0.0;while(!entry.getValue().isEmpty()){ sum+=entry.getValue().poll(); }ans.put(entry.getKey(), sum/5); }return ans; }}