Skip to content

Commit 5584315

Browse files
authored
Problem Difficulty Level: Medium
1 parent 107e06c commit 5584315

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*-----------------------------
2+
Time Complexity: O(V + E)
3+
Space ComplexityL O(V + E)
4+
--------------------------------*/
5+
6+
class Solution {
7+
public:
8+
vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
9+
int n = friends.size();
10+
queue<int> fifo;
11+
vector<bool> visited(n, false);
12+
unordered_map<string, int> freq;
13+
fifo.push(id);
14+
visited[id] = true;
15+
int currLevel = 0;
16+
17+
while(!fifo.empty()){
18+
int size = fifo.size();
19+
if(currLevel == level) break;
20+
21+
for(int i = 0; i < size; i++){
22+
int node = fifo.front();
23+
fifo.pop();
24+
25+
for(int frd : friends[node]){
26+
if(!visited[frd]){
27+
fifo.push(frd);
28+
visited[frd] = true;
29+
}
30+
}
31+
}
32+
currLevel += 1;
33+
}
34+
35+
while(!fifo.empty()){
36+
int node = fifo.front();
37+
fifo.pop();
38+
for(const string& video : watchedVideos[node]) freq[video] += 1;
39+
}
40+
41+
vector<pair<string, int>> freqVector(freq.begin(), freq.end());
42+
sort(freqVector.begin(), freqVector.end(), [](pair<string, int>& a, pair<string, int>& b) {
43+
return a.second == b.second ? a.first < b.first : a.second < b.second;
44+
});
45+
46+
vector<string> ans;
47+
for(auto& p : freqVector) ans.push_back(p.first);
48+
return ans;
49+
}
50+
};
51+
52+
/*
53+
Question Link: https://leetcode.com/problems/get-watched-videos-by-your-friends/
54+
Author: M.R.Naganathan
55+
*/

0 commit comments

Comments
 (0)