Skip to content

Commit 75c597a

Browse files
add one more solution for 1086
1 parent 150be67 commit 75c597a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/main/java/com/fishercoder/solutions/_1086.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.Collections;
55
import java.util.List;
6+
import java.util.PriorityQueue;
67
import java.util.TreeMap;
78

89
/**
@@ -53,4 +54,35 @@ public int[][] highFive(int[][] items) {
5354
return result;
5455
}
5556
}
57+
58+
public static class Solution2 {
59+
public int[][] highFive(int[][] items) {
60+
TreeMap<Integer, PriorityQueue<Integer>> treeMap = new TreeMap<>();
61+
for (int[] studentToScores : items) {
62+
if (treeMap.containsKey(studentToScores[0])) {
63+
PriorityQueue<Integer> maxHeap = treeMap.get(studentToScores[0]);
64+
maxHeap.offer(studentToScores[1]);
65+
if (maxHeap.size() > 5) {
66+
maxHeap.poll();
67+
}
68+
treeMap.put(studentToScores[0], maxHeap);
69+
} else {
70+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
71+
maxHeap.offer(studentToScores[1]);
72+
treeMap.put(studentToScores[0], maxHeap);
73+
}
74+
}
75+
int[][] result = new int[treeMap.size()][2];
76+
for (int id : treeMap.keySet()) {
77+
result[id - 1][0] = id;
78+
int sum = 0;
79+
PriorityQueue<Integer> maxHeap = treeMap.get(id);
80+
while (!maxHeap.isEmpty()) {
81+
sum += maxHeap.poll();
82+
}
83+
result[id - 1][1] = sum / 5;
84+
}
85+
return result;
86+
}
87+
}
5688
}

src/test/java/com/fishercoder/_1086Test.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
public class _1086Test {
1010
private static _1086.Solution1 solution1;
11+
private static _1086.Solution2 solution2;
1112
private static int[][] items;
1213

1314
@BeforeClass
1415
public static void setup() {
1516
solution1 = new _1086.Solution1();
17+
solution2 = new _1086.Solution2();
1618
}
1719

1820
@Test
@@ -36,4 +38,25 @@ public void test1() {
3638
}, solution1.highFive(items));
3739
}
3840

41+
@Test
42+
public void test2() {
43+
items = new int[][]{
44+
{1, 91},
45+
{1, 92},
46+
{2, 93},
47+
{2, 97},
48+
{1, 60},
49+
{2, 77},
50+
{1, 65},
51+
{1, 87},
52+
{1, 100},
53+
{2, 100},
54+
{2, 76}
55+
};
56+
assertArrayEquals(new int[][]{
57+
{1, 87},
58+
{2, 88}
59+
}, solution2.highFive(items));
60+
}
61+
3962
}

0 commit comments

Comments
 (0)