# 1311. Get Watched Videos by Your Friends

There are `n` people, each person has a unique  id  between `0` and `n-1`. Given the arrays `watchedVideos` and `friends`, where `watchedVideos[i]` and `friends[i]` contain the list of watched videos and the list of friends respectively for the person with `id = i`.

Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level `k` of videos are all watched videos by people with the shortest path exactly equal to `k` with you. Given your `id` and the `level` of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.

Example 1:

``````Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
``````

Example 2:

``````Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
``````

Constraints:

• `n == watchedVideos.length == friends.length`
• `2 <= n <= 100`
• `1 <= watchedVideos[i].length <= 100`
• `1 <= watchedVideos[i][j].length <= 8`
• `0 <= friends[i].length < n`
• `0 <= friends[i][j] < n`
• `0 <= id < n`
• `1 <= level < n`
• if `friends[i]` contains `j`, then `friends[j]` contains `i`

``````class Solution {
public:
vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
int n = watchedVideos.size();
vector<string> res;
unordered_map<string, int> videoCnt;
set<pair<int, string>> videoSet;
vector<int> ids, visited(n);
queue<int> q{{id}};
visited[id] = 1;
while (!q.empty() && level >= 0) {
for (int i = q.size(); i > 0; --i) {
int t = q.front(); q.pop();
if (level == 0) ids.push_back(t);
for (int j : friends[t]) {
if (visited[j]) continue;
visited[j] = 1;
q.push(j);
}
}
--level;
}
for (int id : ids) {
for (string video : watchedVideos[id]) {
++videoCnt[video];
}
}
for (auto &a : videoCnt) {
videoSet.insert({a.second, a.first});
}
for (auto &p : videoSet) {
res.push_back(p.second);
}
return res;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1311

https://leetcode.com/problems/get-watched-videos-by-your-friends/

https://leetcode.com/problems/get-watched-videos-by-your-friends/discuss/470695/C%2B%2B-BFS-%2B-Sort

https://leetcode.com/problems/get-watched-videos-by-your-friends/discuss/470748/BFS-%2B-Hash-Map-%2B-Set

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation