-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path1311.cpp
41 lines (38 loc) · 1.1 KB
/
1311.cpp
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
41
class Solution
{
public:
vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level)
{
queue<int> q;
bool seen[friends.size()] = {};
q.push(id), seen[id] = 1;
while (level--)
{
for (int i = q.size(); i > 0; i--)
{
int idx = q.front(); q.pop();
for (int p : friends[idx])
{
if (!seen[p])
{
q.push(p);
seen[p] = 1;
}
}
}
}
unordered_map<string, int> cnt;
while (!q.empty())
{
for (string& s : watchedVideos[q.front()]) cnt[s]++;
q.pop();
}
vector<string> res;
for (auto& it : cnt) res.push_back(it.first);
sort(res.begin(), res.end(), [&](string& a, string& b){
if (cnt[a] == cnt[b]) return a < b;
return cnt[a] < cnt[b];
});
return res;
}
};