Get Watched Videos by Your Friends - Complete Solution Guide
Get Watched Videos by Your Friends is LeetCode problem 1311, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem asks us to find the videos watched by the friends of a person at a specific level (distance) in a social network. We are given the list of videos watched by each person and the list of friends for each person. We need to perform a breadth-first search (BFS) to find all people at the given level and then count the frequency of each video watched by these people. Finally, we return a sorted list of videos based on their frequencies (ascending) and then alphabetically (ascending) for videos with the same frequency.
Solution Approach
The solution uses a BFS algorithm to find all people at the specified `level`. It starts from the given `id` and explores the graph level by level. A `visited` set is maintained to prevent cycles and avoid revisiting people. Once the BFS reaches the desired `level`, it counts the frequencies of all videos watched by those people. Finally, the videos are sorted based on frequency (ascending) and alphabetically (ascending), and the result is returned as a list of videos.
Step-by-Step Algorithm
- Step 1: Initialize a queue with the starting `id` and a set `visited` to keep track of visited nodes.
- Step 2: Perform BFS up to the given `level`. In each level, iterate through the friends of each person in the current queue. If a friend hasn't been visited, add them to the queue and the `visited` set.
- Step 3: After reaching the desired `level`, iterate through the people in the queue (the friends at that level).
- Step 4: For each person at the target level, iterate through their `watchedVideos` and update a counter (hash table) to store the frequency of each video.
- Step 5: Sort the videos based on their frequency (ascending) and alphabetically (ascending) if frequencies are equal.
- Step 6: Extract the video names from the sorted list and return the list of videos.
Key Insights
- Insight 1: Breadth-first search (BFS) is the correct algorithm to traverse the social network graph and find all people at a given distance (level) from the starting person.
- Insight 2: A hash table (or counter) is useful for storing and counting the frequency of each watched video.
- Insight 3: Sorting is needed to order the videos based on their frequencies and alphabetically.
Complexity Analysis
Time Complexity: O(N+VlogV)
Space Complexity: O(N+V)
Topics
This problem involves: Array, Hash Table, Breadth-First Search, Graph, Sorting.
Companies
Asked at: Guidewire.