-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighFive.js
41 lines (39 loc) · 1.4 KB
/
HighFive.js
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
/**
* Variables:
* - scores: hash table to store each student's score with key-val pair of id-scores(array)
* - result: variable that holds each players top 5 average
* Loop through each pair in items array
* - if id already exists in scores hash table, push score to it
* - else, hash scores hash at key id to equal an array with the first score as its first element
* Loop through each key in scores
* - have variable studentScores to hold the value at scores[key]
* - if the length of studentScores is greater than 5
* - sort studentScores in descending order and have s equal to just the first 5 elements of the sorted studentScores array
* - calculate average for student
* - push to results [id, average]
* Return result
*/
const highFive = (items) => {
let scores = {};
let result = [];
for (const item of items) {
const id = item[0],
score = item[1];
scores[id] ? scores[id].push(score) : (scores[id] = [score]);
}
for (const id in scores) {
let studentScores = scores[id];
if (studentScores.length > 5)
studentScores = studentScores.sort((a, b) => b - a).slice(0, 5);
const average = Math.floor(
studentScores.reduce((sum, num) => sum + num, 0) / 5
);
result.push([id, average]);
}
return result;
};
/**
* N - number of elements in items
* Time Complexity: O(NLogN)
* Space Complexity: O(N)
*/