-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1311.获取你好友已观看的视频.js
61 lines (54 loc) · 1.56 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* @lc app=leetcode.cn id=1310 lang=javascript
*
* [1310] 获取你好友已观看的视频
*
* 1. 好友关系构成了无向图, 获取指定层级的好友关系时要排除环
* 2. 使用双数组遍历好友关系, 因为俩人可能是同一人的好友, 因此用了 Set 去重
* 3. 取到 n 层级的人之后, 计算各电影观看次数, 最后做个排序即可
*
*/
/**
* @param {string[][]} watchedVideos
* @param {number[][]} friends
* @param {number} id
* @param {number} level
* @return {string[]}
*/
var watchedVideosByFriends = function(watchedVideos, friends, id, level) {
const set = new Set([id]);
let list1 = new Set([id]);
let list2 = new Set();
while (level > 0) {
const current = list1.size ? list1 : list2;
const next = list1.size ? list2 : list1;
for (const n of current) {
for (const f of friends[n]) {
if (!set.has(f)) {
next.add(f);
set.add(f);
}
}
}
level--;
current.clear();
}
const peoples = Array.from(list1.size ? list1 : list2);
const map = {};
for (const p of peoples) {
for (const w of watchedVideos[p]) {
map[w] = map[w] || 0;
map[w]++;
}
}
const result = Object.entries(map).sort((a, b) => {
if (a[1] < b[1]) {
return -1;
} else if (a[1] > b[1]) {
return 1;
} else {
return a[0] < b[0] ? -1 : 1;
}
}).map(x => x[0]);
return result;
};