Leetcode 1311: Get Watched Videos by Your Friends

grid47
grid47
Exploring patterns and algorithms
Jun 28, 2024 8 min read

You are given n people with unique IDs and two lists: watchedVideos and friends. For a given ID and a level k, find the list of videos watched by people at exactly level k from you. Return the videos ordered by their frequency (in increasing order), and alphabetically if there is a tie in frequency.
Problem
Approach
Steps
Complexity
Input: The input consists of two arrays: `watchedVideos` (list of lists of strings) and `friends` (list of lists of integers). Each list contains videos watched by each person and their corresponding friends, respectively.
Example: Input: watchedVideos = [["Music","Gaming"],["Vlog"],["Music","Gaming"],["Cooking"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Constraints:
• 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
Output: The output is a list of videos sorted first by frequency in increasing order, and alphabetically for ties in frequency.
Example: Output: ["Gaming","Music"]
Constraints:
• The output list must contain videos ordered by frequency and alphabetically for tied frequencies.
Goal: Identify and sort the videos watched by people at exactly level `k` from the given `id`.
Steps:
• Use breadth-first search (BFS) to traverse the graph of friends, identifying people at level `k` from the given `id`.
• Track the frequency of videos watched by people at level `k`.
• Sort the videos based on their frequency and alphabetically for ties.
Goal: Ensure that the solution is efficient for up to 100 people and the corresponding input size constraints.
Steps:
• Ensure the solution works for arrays with up to 100 elements and handles the BFS traversal correctly.
Assumptions:
• The input arrays are valid and all constraints are respected.
Input: Input: watchedVideos = [["Music","Gaming"],["Vlog"],["Music","Gaming"],["Cooking"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Explanation: The BFS traversal identifies the people at level 1 from the given ID (0), and the videos they watched are counted. The result is the list of videos ordered by their frequency.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus