Skip to content

Commit c836cb7

Browse files
authored
Create get-watched-videos-by-your-friends.cpp
1 parent f2e58ce commit c836cb7

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Time: O(n + vlogv), v is the number of the level videos
2+
// Space: O(w)
3+
4+
class Solution {
5+
public:
6+
vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
7+
vector<int> curr_level = {id};
8+
unordered_set<int> lookup = {id};
9+
for (int i = 0; i < level; ++i) {
10+
vector<int> new_level;
11+
for (const auto& i : curr_level) {
12+
for (const auto& j : friends[i]) {
13+
if (lookup.count(j)) {
14+
continue;
15+
}
16+
lookup.emplace(j);
17+
new_level.emplace_back(j);
18+
}
19+
}
20+
curr_level = move(new_level);
21+
}
22+
23+
unordered_map<string, int> count;
24+
for (const auto& i : curr_level) {
25+
for (const auto& v : watchedVideos[i]) {
26+
++count[v];
27+
}
28+
}
29+
vector<string> result;
30+
transform(count.cbegin(), count.cend(), back_inserter(result),
31+
[](const auto& x) {
32+
return x.first;
33+
});
34+
sort(result.begin(), result.end(),
35+
[&count](const auto& a, const auto& b) {
36+
return make_pair(count[a], a) < make_pair(count[b], b);
37+
});
38+
return result;
39+
}
40+
};

0 commit comments

Comments
 (0)