1311. Get Watched Videos by Your Friends
https://leetcode.com/problems/get-watched-videos-by-your-friends/


Previous1298. Maximum Candies You Can Get from BoxesNext1391. Check if There is a Valid Path in a Grid
Last updated
https://leetcode.com/problems/get-watched-videos-by-your-friends/


Last updated
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 -> 2Input: 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).class Solution {
public:
vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
queue<int> q;
q.push(id);
unordered_set<int> visited;
visited.insert(id);
for (int i = 0; i < level && !q.empty(); ++i) {
for (int j = q.size(); j > 0; --j) {
const auto t = q.front(); q.pop();
for (auto &f : friends[t]) {
if (!visited.count(f)) {
q.push(f);
visited.insert(f);
}
}
}
}
unordered_map<string, int> freq;
while (!q.empty()) {
const auto t = q.front(); q.pop();
for (const auto c : watchedVideos[t]) {
++freq[c];
}
}
const auto cmp = [&](auto &a, auto &b) {
if (a.second != b.second) return a.second > b.second;
return a.first > b.first;
};
priority_queue<pair<string, int>, vector<pair<string, int>>, decltype(cmp)> pq(cmp);
for (const auto &p : freq) {
pq.push(p);
}
vector<string> res;
while (!pq.empty()) {
const auto t = pq.top(); pq.pop();
res.push_back(t.first);
}
return res;
}
};