Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 694eb0f

Browse files
add solution for 1086
1 parent 95ef264 commit 694eb0f

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Your ideas/fixes/algorithms are more than welcome!
4444
|1170|[Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1170.java) | | | |Easy||
4545
|1119|[Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1119.java) | | | [:tv:](https://www.youtube.com/watch?v=6KCBrIWEauw)|Easy||
4646
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | O(n^2) | O(1) | |Easy||
47+
|1086|[High Five](https://leetcode.com/problems/high-five/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1086.java) | | | |Easy||
4748
|1085|[Sum of Digits in the Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1085.java) | | | |Easy||
4849
|1079|[Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1079.java) | O(1) | O(1) | |Medium||
4950
|1078|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1078.java) | O(n) | O(1) | |Easy||
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.TreeMap;
9+
10+
/**
11+
* 1086. High Five
12+
*
13+
* Given a list of scores of different students, return the average score of each student's top five scores in the order of each student's id.
14+
* Each entry items[i] has items[i][0] the student's id, and items[i][1] the student's score.
15+
* The average score is calculated using integer division.
16+
*
17+
* Example 1:
18+
* Input: [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]]
19+
* Output: [[1,87],[2,88]]
20+
* Explanation:
21+
* The average of the student with id = 1 is 87.
22+
* The average of the student with id = 2 is 88.6. But with integer division their average converts to 88.
23+
*
24+
* Note:
25+
* 1 <= items.length <= 1000
26+
* items[i].length == 2
27+
* The IDs of the students is between 1 to 1000
28+
* The score of the students is between 1 to 100
29+
* For each student, there are at least 5 scores
30+
* */
31+
public class _1086 {
32+
public static class Solution1 {
33+
public int[][] highFive(int[][] items) {
34+
TreeMap<Integer, List<Integer>> map = new TreeMap<>();
35+
for (int[] studentToScore : items) {
36+
if (map.containsKey(studentToScore[0])) {
37+
map.get(studentToScore[0]).add(studentToScore[1]);
38+
} else {
39+
List<Integer> list = new ArrayList<>();
40+
list.add(studentToScore[1]);
41+
map.put(studentToScore[0], list);
42+
}
43+
}
44+
int[][] result = new int[map.size()][2];
45+
for (int id : map.keySet()) {
46+
List<Integer> scores = map.get(id);
47+
Collections.sort(scores);
48+
int sum = 0;
49+
for (int i = scores.size() - 1; i >= scores.size() - 5 && i >= 0; i--) {
50+
sum += scores.get(i);
51+
}
52+
result[id - 1][0] = id;
53+
result[id - 1][1] = sum / 5;
54+
}
55+
return result;
56+
}
57+
}
58+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1086;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1086Test {
10+
private static _1086.Solution1 solution1;
11+
private static int[][] items;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1086.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
items = new int[][]{
21+
{1, 91},
22+
{1, 92},
23+
{2, 93},
24+
{2, 97},
25+
{1, 60},
26+
{2, 77},
27+
{1, 65},
28+
{1, 87},
29+
{1, 100},
30+
{2, 100},
31+
{2, 76}
32+
};
33+
assertArrayEquals(new int[][]{
34+
{1, 87},
35+
{2, 88}
36+
}, solution1.highFive(items));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)