zoukankan      html  css  js  c++  java
  • 1311. Get Watched Videos by Your Friends

    问题:

    朋友看视频问题。

    给定一个朋友关系网,

    friends[i]表示 i 的朋友们。

    watchedVideos[i]表示 i 所看的视频。

    level表示朋友层,1:代表i的朋友,2:代表i的朋友的朋友。

    求给定人 i 的朋友层level,所看过的所有视频。(按照视频被看的人数从少到多排序)

    Example 1:
    Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
    Output: ["B","C"] 
    Explanation: 
    You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
    Person with id = 1 -> watchedVideos = ["C"] 
    Person with id = 2 -> watchedVideos = ["B","C"] 
    The frequencies of watchedVideos by your friends are: 
    B -> 1 
    C -> 2
    
    Example 2:
    Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
    Output: ["D"]
    Explanation: 
    You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
     
    Constraints:
    n == watchedVideos.length == friends.length
    2 <= n <= 100
    1 <= watchedVideos[i].length <= 100
    1 <= watchedVideos[i][j].length <= 8
    0 <= friends[i].length < n
    0 <= friends[i][j] < n
    0 <= id < n
    1 <= level < n
    if friends[i] contains j, then friends[j] contains i
    

      

    example 1:

    example 2:

    解法:BFS

    状态:当前人 id

    visited:保存遍历过的人 id

    level按照queue遍历层数进行递减。

    当level递减到0,开始记录该层人所看过的视频。

    存入unordered_map中,key:视频,value:看过的人数

    每层遍历完,如果level==0,退出遍历。

    将结果存入set中,进行排序(first:看过的人数。second:视频)

    再将set转存入结果vector<string> 即为已排过序的结果。

    代码参考:

     1 class Solution {
     2 public:
     3     vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
     4         queue<int> q;
     5         unordered_set<int> visited;
     6         unordered_map<string,int> res_map;
     7         vector<string> res;
     8         q.push(id);
     9         visited.insert(id);
    10         while(!q.empty()) {
    11             int sz = q.size();
    12             for(int i=0; i<sz; i++) {
    13                 int cur = q.front();
    14                 q.pop();
    15                 //cout<<"pop:"<<cur<<" level:"<<level<<endl;
    16                 if(level==0) {
    17                     for(auto vd:watchedVideos[cur])
    18                         res_map[vd]++;
    19                 } else {
    20                     for(auto frd:friends[cur]) {
    21                         if(visited.insert(frd).second) {
    22                             q.push(frd);
    23                         }
    24                     }
    25                 }
    26             }
    27             if(level==0) break;
    28             level--;
    29         }
    30         set<pair<int,string>> res_set;
    31         for(auto rm:res_map) {
    32             res_set.insert({rm.second,rm.first});
    33         }
    34         for(auto rs:res_set) {
    35             res.push_back(rs.second);
    36         }
    37         return res;
    38     }
    39 };
  • 相关阅读:
    SharePoint Portal Server与SharePoint Services之间的关系
    配置Microsoft Visual SourceSafe 2005的局域网/Internet访
    Maven创建Web项目(idea)
    Maven入门(idea)
    Windows下搭建Vue脚手架CLI
    关于wince中的全屏显示
    ubuntu右键添加打开终端的快捷菜单[转]
    将CString转换成string ...
    090606日记
    Java中的InputStream和OutputStream
  • 原文地址:https://www.cnblogs.com/habibah-chang/p/14562602.html
Copyright © 2011-2022 走看看