【Leetcode】1311. Get Watched Videos by Your Friends

题目地址:

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

给定一个 n n n阶无权无向简单图,以邻接表形式给出,每个顶点代表一个人,两个人之间有边代表这两个人是朋友关系。若从一个人 A A A到达 B B B至少需要 k k k步,就称 B B B A A A k k k阶朋友。再给定每个人看过的视频的列表,视频以字符串给出。给定一个id和一个整数 k k k,问这个id的人的 k k k阶朋友看过的视频是哪些,要求返回的时候按照视频在他的 k k k阶朋友里的观看次数升序排序,若有次数相等的则按照字典序排序。

法1:BFS。先用BFS求出此人的 k k k阶朋友,然后得到他们所看的视频以及频率,再将这些视频排序即可。代码如下:

import java.util.*;

public class Solution {
    public List<String> watchedVideosByFriends(List<List<String>> watchedVideos, int[][] friends, int id, int level) {
        List<String> res = new ArrayList<>();
        
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(id);
        Set<Integer> visited = new HashSet<>();
        visited.add(id);
        
        int curLevel = 0;
        Set<Integer> friendsIds = new HashSet<>();
        while (!queue.isEmpty()) {
            curLevel++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int cur = queue.poll();
                for (int next : friends[cur]) {
                    if (visited.contains(next)) {
                        continue;
                    }
                    
                    if (curLevel == level) {
                        friendsIds.add(next);
                    }
                    
                    if (curLevel < level) {
                        visited.add(next);
                        queue.offer(next);
                    }
                }
            }
            
            // 搜到了第level层就退出
            if (curLevel == level) {
                break;
            }
        }
    
        Map<String, Integer> count = new HashMap<>();
        for (int i = 0; i < watchedVideos.size(); i++) {
            if (friendsIds.contains(i)) {
                for (String video : watchedVideos.get(i)) {
                    count.put(video, count.getOrDefault(video, 0) + 1);
                }
            }
        }
        
        res.addAll(count.keySet());
        res.sort((m1, m2) -> !count.get(m1).equals(count.get(m2)) ? Integer.compare(count.get(m1), count.get(m2)) : m1.compareTo(m2));
        
        return res;
    }    
}

时间复杂度 O ( V + E + s l log ⁡ l ) O(V+E+sl\log l) O(V+E+sllogl) l l l是视频数量, s s s是视频字符串最长长度。空间 O ( V + s l ) O(V+sl) O(V+sl)

法2:SPFA。只需额外维护一个数组记录每个顶点到达的时候的最小步数。代码如下:

import java.util.*;

public class Solution {
    public List<String> watchedVideosByFriends(List<List<String>> watchedVideos, int[][] friends, int id, int level) {
        int[] dis = new int[friends.length];
        Arrays.fill(dis, Integer.MAX_VALUE);
        dis[id] = 0;
        int[] plen = new int[friends.length];
        
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(id);
        // fids存储第level层的朋友编号
        Set<Integer> fids = new HashSet<>();

        while (!queue.isEmpty()) {
            int cur = queue.poll();
            for (int next : friends[cur]) {
                if (dis[cur] != Integer.MAX_VALUE && plen[cur] < level && dis[next] > dis[cur] + 1) {
                    dis[next] = dis[cur] + 1;
                    plen[next] = plen[cur] + 1;
                    
                    if (plen[next] == level) {
                        fids.add(next);
                    }
                    
                    queue.offer(next);
                }
            }
        }
        
        Map<String, Integer> count = new HashMap<>();
        for (int fid : fids) {
            for (String vid : watchedVideos.get(fid)) {
                count.put(vid, count.getOrDefault(vid, 0) + 1);
            }
        }
    
        List<String> res = new ArrayList<>(count.keySet());
        res.sort((m1, m2) -> !count.get(m1).equals(count.get(m2)) ? Integer.compare(count.get(m1), count.get(m2)) : m1.compareTo(m2));
    
        return res;
    }
}

时间复杂度 O ( V E + s l log ⁡ l ) O(VE+sl\log l) O(VE+sllogl) l l l是视频数量, s s s是视频字符串最长长度。空间 O ( V + s l ) O(V+sl) O(V+sl)

C++:

class Solution {
 public:
  vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos,
                                        vector<vector<int>>& friends, int id,
                                        int k) {
    int n = friends.size();
    queue<int> q;
    int dist[n];
    memset(dist, -1, sizeof dist);
    dist[id] = 0;
    q.push(id);
    while (q.size()) {
      int t = q.front();
      q.pop();
      for (int x : friends[t]) {
        if (dist[x] == -1) {
          dist[x] = dist[t] + 1;
          q.push(x);
        }
      }
    }

    unordered_map<string, int> mp;
    for (int i = 0; i < n; i++)
      if (dist[i] == k)
        for (auto& s : watchedVideos[i]) mp[s]++;

    pair<int, string> items[mp.size()];
    n = 0;
    for (auto& [k, v] : mp) items[n++] = {v, k};
    sort(items, items + n);
    vector<string> res;
    for (auto& p : items) res.push_back(p.second);
    return res;
  }
};

时空复杂度一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值