forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC1311.cpp
More file actions
executable file
·40 lines (35 loc) · 986 Bytes
/
LC1311.cpp
File metadata and controls
executable file
·40 lines (35 loc) · 986 Bytes
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
/*
Problem Statement: https://leetcode.com/problems/get-watched-videos-by-your-friends/
*/
class Solution {
public:
vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
int size, n = friends.size();
queue<int> q;
vector<string> movies;
vector<bool> visited(n);
set<pair<int, string>> s;
unordered_map<string, int> m;
level++;
q.push(id);
visited[id] = true;
while (level-- && !q.empty()) {
size = q.size();
while (size--) {
id = q.front();
q.pop();
for (string& movie: watchedVideos[id])
if (level == 0)
m[movie]++;
for (int v: friends[id])
if (!visited[v]) {
q.push(v);
visited[v] = true;
}
}
}
transform(m.begin(), m.end(), inserter(s, s.end()), [](auto& p) { return make_pair(p.second, p.first); });
transform(s.begin(), s.end(), back_inserter(movies), [](auto& p) { return p.second; });
return movies;
}
};