forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1311.js
36 lines (32 loc) · 873 Bytes
/
1311.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
var watchedVideosByFriends = function(watchedVideos, friends, id, level) {
let seen = new Array(friends.length);
seen.fill(0);
seen[id] = 1;
let q = [id];
while (level--) {
for (let i = q.length; i > 0; i--) {
let idx = q.shift();
for (let p of friends[idx]) {
if (seen[p] == 0) {
seen[p] = 1;
q.push(p);
}
}
}
}
let cnt = new Map();
for (let v of q) {
for (let s of watchedVideos[v]) {
cnt.set(s, (cnt.get(s) || 0) + 1);
}
}
let res = [];
for (let k of cnt.keys()) {
res.push(k);
}
res.sort((a, b) => {
if (cnt.get(a) == cnt.get(b)) return a < b ? -1 : a == b ? 0 : 1;
else return cnt.get(a) - cnt.get(b);
});
return res;
};