Leetcode Problem 1311. Get Watched Videos by Your Friends

1311. Get Watched Videos by Your Friends

Leetcode Solutions

BFS Traversal and Frequency Sorting

  1. Initialize a queue and add the given id to it.
  2. Initialize a visited set to keep track of visited friends.
  3. Perform BFS up to the given level.
    • In each iteration, for the current level, iterate over the queue.
    • For each friend in the queue, add their friends to the queue if not visited.
  4. Once the desired level is reached, iterate over the friends at that level and count the frequency of each video.
  5. Sort the videos by frequency and then alphabetically.
  6. Return the sorted list of videos.
UML Thumbnail

DFS Traversal with Level Limit

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...