Get Watched Videos by Your Friends LeetCode solution in JavaScript

1
/**
2
* @param {string[][]} watchedVideos
3
* @param {number[][]} friends
4
* @param {number} id
5
* @param {number} level
6
* @return {string[]}
7
*/
8
var watchedVideosByFriends = function (watchedVideos, friends, id, level) {
9
const queue = [];
10
queue.push([id, 0]);
11
const seen = new Set();
12
const map = {};
13

14
while (queue.length > 0) {
15
const [id, _level] = queue.shift();
16
if (seen.has(id)) {
17
continue;
18
}
19
seen.add(id);
20
if (_level == level) {
21
for (const video of watchedVideos[id]) {
22
if (!map[video]) {
23
map[video] = 1;
24
} else {
25
map[video] += 1;
26
}
27
}
28
continue;
29
}
30

31
for (const f of friends[id]) {
32
queue.push([f, _level + 1]);
33
}
34
}
35

36
return Object.keys(map).sort((a, b) => {
37
const diff = map[a] - map[b];
38
if (diff == 0) {
39
return a.localeCompare(b);
40
}
41
return diff;
42
});
43
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0