forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1086.java
67 lines (64 loc) · 2.48 KB
/
_1086.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.TreeMap;
public class _1086 {
public static class Solution1 {
public int[][] highFive(int[][] items) {
TreeMap<Integer, List<Integer>> map = new TreeMap<>();
for (int[] studentToScore : items) {
if (map.containsKey(studentToScore[0])) {
map.get(studentToScore[0]).add(studentToScore[1]);
} else {
List<Integer> list = new ArrayList<>();
list.add(studentToScore[1]);
map.put(studentToScore[0], list);
}
}
int[][] result = new int[map.size()][2];
for (int id : map.keySet()) {
List<Integer> scores = map.get(id);
Collections.sort(scores);
int sum = 0;
for (int i = scores.size() - 1; i >= scores.size() - 5 && i >= 0; i--) {
sum += scores.get(i);
}
result[id - 1][0] = id;
result[id - 1][1] = sum / 5;
}
return result;
}
}
public static class Solution2 {
public int[][] highFive(int[][] items) {
TreeMap<Integer, PriorityQueue<Integer>> treeMap = new TreeMap<>();
for (int[] studentToScores : items) {
if (treeMap.containsKey(studentToScores[0])) {
PriorityQueue<Integer> maxHeap = treeMap.get(studentToScores[0]);
maxHeap.offer(studentToScores[1]);
if (maxHeap.size() > 5) {
maxHeap.poll();
}
treeMap.put(studentToScores[0], maxHeap);
} else {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
maxHeap.offer(studentToScores[1]);
treeMap.put(studentToScores[0], maxHeap);
}
}
int[][] result = new int[treeMap.size()][2];
for (int id : treeMap.keySet()) {
result[id - 1][0] = id;
int sum = 0;
PriorityQueue<Integer> maxHeap = treeMap.get(id);
while (!maxHeap.isEmpty()) {
sum += maxHeap.poll();
}
result[id - 1][1] = sum / 5;
}
return result;
}
}
}