Get Watched Videos by Your Friends Problem
Description
LeetCode Problem 1311.
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.length2 <= n <= 1001 <= watchedVideos[i].length <= 1001 <= watchedVideos[i][j].length <= 80 <= friends[i].length < n0 <= friends[i][j] < n0 <= id < n1 <= level < n- if
friends[i]containsj, thenfriends[j]containsi
Solution
Approach: BFS, Then Count and Sort
The people at level k are those whose shortest path from id has exactly k edges. A breadth-first search finds them: start with the frontier [id], and replace it level times by the people adjacent to the current frontier who have not been seen yet.
Marking people as seen when they are first reached is the important part. A person can often be reached along several paths: in the examples, person 3 is a friend of both 1 and 2, and you are a friend of your own friends. With the marks, each person is counted once, only in the frontier of their shortest distance, and id itself never shows up at a later level.
After level rounds, the frontier is the set of people at that level (possibly empty, if the level is beyond everyone reachable). Count how often each video appears in their lists, then sort the videos by (count, name): increasing frequency, ties broken alphabetically.
Complexity: O(n + e + v log v) time, where e is the number of friendships and v the number of videos watched at that level; O(n + v) space.
from collections import Counter
class Solution:
def watchedVideosByFriends(self, watchedVideos: List[List[str]], friends: List[List[int]], id: int, level: int) -> List[str]:
# BFS one level at a time; marking people when they are first reached keeps each at its shortest distance.
seen = {id}
frontier = [id]
for _ in range(level):
nxt = []
for person in frontier:
for f in friends[person]:
if f not in seen:
seen.add(f)
nxt.append(f)
frontier = nxt
count = Counter(v for person in frontier for v in watchedVideos[person])
return sorted(count, key=lambda v: (count[v], v))