leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1311.cpp (1152B)


0 class Solution { 1 public: 2 vector<string> watchedVideosByFriends(vector<vector<string>> &watchedVideos, vector<vector<int>> &adj, 3 int id, int level) { 4 int n = adj.size(); 5 vector<bool> visited(n, false); 6 queue<int> q; 7 8 q.push(id); 9 visited[id] = true; 10 for (int lvl = 0; lvl != level; lvl++) { 11 for (int k = q.size(); k > 0; k--) { 12 int id = q.front(); 13 q.pop(); 14 for (int c : adj[id]) { 15 if (!visited[c]) { 16 visited[c] = true; 17 q.push(c); 18 } 19 } 20 } 21 } 22 23 unordered_map<string, int> freq; 24 vector<pair<int, string>> vec; 25 vector<string> res; 26 27 for (; !q.empty(); q.pop()) 28 for (auto &st : watchedVideos[q.front()]) 29 freq[st]++; 30 31 for (auto &[k, v] : freq) 32 vec.push_back({v, k}); 33 sort(vec.begin(), vec.end()); 34 for (auto &[_, title] : vec) 35 res.push_back(title); 36 return res; 37 } 38 };