• 1311. 获取你好友已观看的视频 BFS


    n 个人,每个人都有一个  0 到 n-1 的唯一 id 。

    给你数组 watchedVideos  和 friends ,其中 watchedVideos[i]  和 friends[i] 分别表示 id = i 的人观看过的视频列表和他的好友列表。

    Level 1 的视频包含所有你好友观看过的视频,level 2 的视频包含所有你好友的好友观看过的视频,以此类推。一般的,Level 为 k 的视频包含所有从你出发,最短距离为 k 的好友观看过的视频。

    给定你的 id  和一个 level 值,请你找出所有指定 level 的视频,并将它们按观看频率升序返回。如果有频率相同的视频,请将它们按字母顺序从小到大排列。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/get-watched-videos-by-your-friends
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    注意bfs的使用,题目要求具体层数时,要用一个变量_遍历层数,并且同span记录上一层中队列的大小。
    关键代码,BFS

    queue <int> q;
            q.push(id);
            vis[id] = true;
            for (int _ = 1; _ <= level; _++) {
                int span = q.size();
                for (int i = 0; i < span; i++) {
                    int u = q.front();
                    q.pop();
                    for (int v : friends[u]) {
                        if (!vis[v]) {
                            q.push(v);
                            vis[v] = true;
                        }
                    }
                }
            }
    
    class Solution {
    public:
        struct Node {
            string video;
            int freq;
            Node (string video, int freq): video(video), freq(freq) {}
            bool operator < (Node obj) const{
                if (freq == obj.freq) {
                    return video < obj.video;
                }
                return freq < obj.freq;
            }
        };
    
        vector <Node> v;
    
        vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
            int n = friends.size();
            unordered_map <int, bool> vis;
            
            queue <int> q;
            q.push(id);
            vis[id] = true;
            for (int _ = 1; _ <= level; _++) {
                int span = q.size();
                for (int i = 0; i < span; i++) {
                    int u = q.front();
                    q.pop();
                    for (int v : friends[u]) {
                        if (!vis[v]) {
                            q.push(v);
                            vis[v] = true;
                        }
                    }
                }
            }
    
            unordered_map <string, int> freq;
            while (!q.empty()) {
                int u = q.front();
                q.pop();
                for (auto video : watchedVideos[u]) {
                    freq[video]++;
                }
            }
    
            for (auto it = freq.begin(); it != freq.end(); it++) {
                v.push_back(Node(it->first, it->second));
            }
            sort(v.begin(), v.end());
    
            vector <string> ret;
            for (auto x : v) {
                ret.push_back(x.video);
            }
    
            return ret;
    
        }
    };
    
  • 相关阅读:
    (C/C++)区别:数组与指针,指针与引用
    C++中数组名和指针的区别联系
    C++引用的用处
    C++编写DLL动态链接库的步骤与实现方法
    C++_编写动态链接库
    C++ 模板
    C++ 信号处理
    C++ 多线程
    js事件冒泡
    js事件委托
  • 原文地址:https://www.cnblogs.com/xgbt/p/14144081.html
Copyright © 2020-2023  润新知