- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighFive.java
More file actions
Latest commit
51 lines (46 loc) · 1.63 KB
/
Copy pathHighFive.java
File metadata and controls
51 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
importjava.util.Comparator;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.PriorityQueue;
publicclassHighFive {
/**
Description
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.
Example
Given results = [[1,91],[1,92],[2,93],[2,99],[2,98],[2,97],[1,60],[1,58],[2,100],[1,61]]
Return
*/
classRecord {
publicintid, score;
publicRecord(intid, intscore){
this.id = id;
this.score = score;
}
}
publicMap<Integer, Double> highFive(Record[] results) {
if (results == null || results.length == 0) returnnull;
Map<Integer, PriorityQueue<Integer>> idToScores = newHashMap<>();
Map<Integer, Double> res = newHashMap<>();
for (inti = 0; i < results.length; i++) {
Recordr = results[i];
if (!idToScores.containsKey(r.id)) {
idToScores.put(r.id, newPriorityQueue<>(5, newComparator<Integer>() {
@Override
publicintcompare(Integero1, Integero2) {
returno2 - o1;
}
}));
}
idToScores.get(r.id).add(r.score);
}
for (intkey : idToScores.keySet()) {
PriorityQueue<Integer> pq = idToScores.get(key);
doublesum = 0;
while (!pq.isEmpty()) {
sum += pq.poll();
}
res.put(key, sum / 5);
}
returnres;
}
}