Show problems as
Classic problem #1311MediumArrayHash TableBreadth-First SearchGraph TheorySorting

Get Watched Videos by Your Friends

Real-world version: Book Club Reading Ring
Tracked in this browser only
Read Get Watched Videos by Your Friends on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A reading club has n members numbered from 0. readBooks[i] lists the titles member i has logged, and a member who logged the same title twice has it listed twice. contacts[i] lists the members that member i swaps books with directly; the relation runs both ways, so if j appears in contacts[i] then i appears in contacts[j].

The rank of a member is the fewest direct swaps needed to pass a book from member member to them, so member has rank 0 and each direct contact of member has rank 1.

Collect every logged entry of every member of rank exactly level, counting each entry separately, so a title logged twice by one member and once by another has been logged three times in that rank. Return the distinct titles from that collection, ordered by how many times they were logged from fewest to most, with titles that were logged the same number of times ordered alphabetically. Return an empty list when no member has that rank.

Examples

Example 1

Input
readBooks = [["reef"], ["kiln", "reef"], ["kiln"], ["moss"]], contacts = [[1, 2], [0], [0], []], member = 0, level = 1
Output
["reef", "kiln"]

Members 1 and 2 have rank 1. Their logs hold kiln twice and reef once, so reef comes first with one logging and kiln second with two.

Example 2

Input
readBooks = [["one", "two"], ["two", "three"], ["three", "one"], ["four"]], contacts = [[1, 2, 3], [0], [0], [0]], member = 0, level = 1
Output
["four", "one", "two", "three"]

Members 1, 2 and 3 have rank 1. Between them three was logged twice, while four, one and two were each logged once, so those three come first in alphabetical order and three comes last.

Example 3

Input
readBooks = [["reef"], ["kiln", "reef"], ["kiln"], ["moss"]], contacts = [[1, 2], [0], [0], []], member = 0, level = 2
Output
[]

Members 1 and 2 are the only contacts of member 0, and member 3 has no contacts at all, so no member has rank 2 and nothing is collected.

Constraints

  • n == readBooks.length == contacts.length
  • 2 <= n <= 100
  • 1 <= readBooks[i].length <= 100
  • 1 <= readBooks[i][j].length <= 8
  • readBooks[i][j] consists of lowercase English letters.
  • 0 <= contacts[i].length < n
  • 0 <= contacts[i][j] < n
  • contacts[i] never contains i and never repeats a member.
  • If contacts[i] contains j, then contacts[j] contains i.
  • 0 <= member < n
  • 1 <= level < n

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def titles_at_distance(readBooks: list[list[str]], contacts: list[list[int]], member: int, level: int) -> list[str]:
Java
public List<String> titlesAtDistance(List<List<String>> readBooks, int[][] contacts, int member, int level)