-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1311.cpp
More file actions
39 lines (35 loc) · 1.13 KB
/
Copy path1311.cpp
File metadata and controls
39 lines (35 loc) · 1.13 KB
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
class Solution {
public:
vector<string> watchedVideosByFriends(vector<vector<string>> &watchedVideos, vector<vector<int>> &adj,
int id, int level) {
int n = adj.size();
vector<bool> visited(n, false);
queue<int> q;
q.push(id);
visited[id] = true;
for (int lvl = 0; lvl != level; lvl++) {
for (int k = q.size(); k > 0; k--) {
int id = q.front();
q.pop();
for (int c : adj[id]) {
if (!visited[c]) {
visited[c] = true;
q.push(c);
}
}
}
}
unordered_map<string, int> freq;
vector<pair<int, string>> vec;
vector<string> res;
for (; !q.empty(); q.pop())
for (auto &st : watchedVideos[q.front()])
freq[st]++;
for (auto &[k, v] : freq)
vec.push_back({v, k});
sort(vec.begin(), vec.end());
for (auto &[_, title] : vec)
res.push_back(title);
return res;
}
};