File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 3
3
import java .util .ArrayList ;
4
4
import java .util .Collections ;
5
5
import java .util .List ;
6
+ import java .util .PriorityQueue ;
6
7
import java .util .TreeMap ;
7
8
8
9
/**
@@ -53,4 +54,35 @@ public int[][] highFive(int[][] items) {
53
54
return result ;
54
55
}
55
56
}
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
+ }
56
88
}
Original file line number Diff line number Diff line change 8
8
9
9
public class _1086Test {
10
10
private static _1086 .Solution1 solution1 ;
11
+ private static _1086 .Solution2 solution2 ;
11
12
private static int [][] items ;
12
13
13
14
@ BeforeClass
14
15
public static void setup () {
15
16
solution1 = new _1086 .Solution1 ();
17
+ solution2 = new _1086 .Solution2 ();
16
18
}
17
19
18
20
@ Test
@@ -36,4 +38,25 @@ public void test1() {
36
38
}, solution1 .highFive (items ));
37
39
}
38
40
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
+
39
62
}
You can’t perform that action at this time.
0 commit comments