-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathproblem7.js
More file actions
40 lines (32 loc) · 1.09 KB
/
problem7.js
File metadata and controls
40 lines (32 loc) · 1.09 KB
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
function problem7(user, friends, visitors) {
const map_friends = new Map();
const map_scores = new Map();
friends.forEach(([a,b]) => {
if (!map_friends.has(a)) map_friends.set(a, new Set());
if (!map_friends.has(b)) map_friends.set(b, new Set());
map_friends.get(a).add(b);
map_friends.get(b).add(a);
});
const user_friends = map_friends.get(user) || new Set();
map_friends.forEach((set_friends, friend) => {
if (friend === user || user_friends.has(friend)) return;
let count = 0;
set_friends.forEach(person => {
if (user_friends.has(person)) ++count;
});
if (count > 0)
map_scores.set(friend, (map_scores.get(friend) || 0) + count * 10);
});
visitors.forEach(visitor => {
if (visitor !== user && !user_friends.has(visitor)){
map_scores.set(visitor, (map_scores.get(visitor) || 0) + 1);
}
});
const ret = Array.from(map_scores.entries())
.filter(([, score]) => score > 0)
.sort((a,b) => b[1]-a[1] || a[0].localeCompare(b[0]))
.slice(0,5)
.map(([friend]) => friend);
return ret;
}
module.exports = problem7;