Get Watched Videos by Your Friends LeetCode solution in C++

1
class Solution {
2
public:
3
vector<string> watchedVideosByFriends(vector<vector<string>> &watchedVideos,
4
vector<vector<int>> &friends, int id, int level) {
5
vector<vector<int>> graph(watchedVideos.size());
6

7
for (int i = 0; i != friends.size(); i++)
8
for (auto &f : friends[i]) graph[f].push_back(i), graph[i].push_back(f);
9

10
int tmp_level = 0;
11
vector<int> vis(watchedVideos.size(), 0);
12
vector<string> ans;
13

14
queue<int> q;
15
q.push(id);
16
while (!q.empty()) {
17
if (tmp_level++ == level) {
18
unordered_map<string, int> mp;
19
while (!q.empty()) {
20
int t = q.front();
21
q.pop();
22
if (vis[t]) continue;
23
vis[t] = 1;
24
for (auto &v : watchedVideos[t]) mp[v]++;
25
}
26
set<pair<int, string>> st;
27
for (auto &[s, n] : mp) st.insert({n, s});
28
for (auto &it : st) ans.push_back(it.second);
29
}
30

31
int n = q.size();
32
while (n--) {
33
int t = q.front();
34
q.pop();
35
if (vis[t]) continue;
36
vis[t] = 1;
37

38
for (auto &x : graph[t])
39
if (!vis[x]) q.push(x);
40
}
41
}
42

43
return ans;
44
}
45
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0