-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathproblem7.js
More file actions
54 lines (44 loc) · 1.1 KB
/
problem7.js
File metadata and controls
54 lines (44 loc) · 1.1 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function problem7(user, friends, visitors) {
const friendList = new Set();
const score = new Map();
for (const [friend1, friend2] of friends) {
if (friend1 === user) {
friendList.add(friend2);
}
else if (friend2 === user) {
friendList.add(friend1);
}
}
for (const [friend1, friend2] of friends) {
if (friend1 !== user && friend2 !== user) {
if (friendList.has(friend1)) {
score.set(friend2, (score.get(friend2) || 0)+10);
}
if (friendList.has(friend2)) {
score.set(friend1, (score.get(friend1) || 0)+10);
}
}
}
for (const visitor of visitors) {
if (!friendList.has(visitor) && visitor !== user) {
score.set(visitor, (score.get(visitor) || 0)+1);
}
}
const sorted = Array.from(score).sort((a, b) => {
if (b[1] !== a[1]) {
return b[1]-a[1];
}
return a[0].localeCompare(b[0]);
});
const result = [];
for (const [name, score_] of sorted) {
if (result.length === 5) {
break;
}
if (score_ > 0) {
result.push(name);
}
}
return result;
}
module.exports = problem7;