forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHigh Five.java
More file actions
37 lines (29 loc) · 1008 Bytes
/
High Five.java
File metadata and controls
37 lines (29 loc) · 1008 Bytes
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
class Solution {
public int[][] highFive(int[][] items) {
final int COUNT = 5;
Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();
Set<Integer> set = new TreeSet<>();
for (int[] item : items) {
int id = item[0];
int score = item[1];
map.computeIfAbsent(id, k -> new PriorityQueue<>(COUNT, Comparator.naturalOrder())).add(score);
if (map.get(id).size() > COUNT) {
map.get(id).poll();
}
set.add(id);
}
int[][] ans = new int[map.size()][2];
int idx = 0;
Iterator<Integer> iterator = set.iterator();
while (iterator.hasNext()) {
int id = iterator.next();
PriorityQueue<Integer> scores = map.get(id);
int sum = 0;
while (!scores.isEmpty()) {
sum += scores.poll();
}
ans[idx++] = new int[]{id, sum / COUNT};
}
return ans;
}
}