Trains the technique from
LeetCode 1311Get Watched Videos by Your FriendsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
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.
Example 1
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
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
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.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def titles_at_distance(readBooks: list[list[str]], contacts: list[list[int]], member: int, level: int) -> list[str]:public List<String> titlesAtDistance(List<List<String>> readBooks, int[][] contacts, int member, int level)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.