1311. Get Watched Videos by Your Friends**

1311. Get Watched Videos by Your Friends**

https://leetcode.com/problems/get-watched-videos-by-your-friends/

题目描述

There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.

Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.

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

C++ 实现 1

BFS 实现, 这道题不难, 可以练习一下 BFS 的基本写法. 使用 queue 实现. 注意已经访问过的人就不要再访问了, 使用 unordered_set<int> seen 来记录已经访问过的人.

class Solution {
public:
    vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
        unordered_set<int> seen{id};
        unordered_map<string, int> record;
        vector<pair<string, int>> results;
        queue<int> Q;
        Q.push(id);
        int k = 0;
        // 最后 k < level, 当结束 k = level - 1 的循环时, Q 中保存着第 level 层的朋友
        for (int k = 0; k < level; ++ k) {
            int size = Q.size();
            while (!Q.empty() && size--) {
                auto cur = Q.front();
                Q.pop();
                for (auto &f : friends[cur]) {
                    if (!seen.count(f)) {
                        seen.insert(f);
                        Q.push(f);
                    }
                }
            }
        }
        while (!Q.empty()) {
            auto cur = Q.front();
            Q.pop();
            for (auto &v : watchedVideos[cur])
                record[v] ++;
        }
        for (auto &p : record) results.push_back(p);
        std::sort(results.begin(), results.end(), [](const pair<string, int> &p,
                    const pair<string, int> &q) { return p.second < q.second || (p.second == q.second && p.first < q.first); });
        vector<string> res;
        for (auto &p : results) res.push_back(p.first);
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值