Viewers With Matching Recent Watches
NetflixPhone screenMedium
You are given a ViewingHistory class built from histories, a dictionary mapping each user id to that user's watch history: movie ids in the order the user watched them, oldest first. A movie may appear more than once in one history.
A user's recent movies for k are the distinct movies among the last k entries of their history. A shorter history uses all its entries, and an empty history gives the empty set. Order and repeats do not matter.
Histories never change after construction.
Part 1: Group users by recent movies
Implement groups(k). It returns every group of two or more users whose recent movies for k are exactly the same set.
Each group lists its user ids in ascending order, and the groups are sorted in ascending order (compared as lists). Users that match nobody are left out.
- Example 1
- init
[{"cy":[2,4,3,7],"ann":[1,2,3,4],"bob":[9,3,4,2],"dee":[5,6],"eve":[6,5],"fay":[4,4,3,2],"gus":[8]}]- operations
[["groups",[3]],["groups",[2]],["groups",[1]],["groups",[10]]]- Output
[[["ann","bob","fay"],["dee","eve"]],[["dee","eve"]],[["bob","fay"]],[["dee","eve"]]]
Why: With
k = 3,ann,bobandfayall have the recent set {2, 3, 4} despite different orders and a repeat, anddeeandeveboth have {5, 6};gusmatches nobody. Fork = 2onlydeeandevestill match, fork = 1onlybobandfayshare the movie 2, and fork = 10the full histories again match onlydeeandeve.
Constraints
0 <= len(histories) <= 10^41 <= k <= 10^40 <= len(histories[u]) <= 10^4for each useru, with at most10^5entries in total1 <= histories[u][i] <= 10^9- Each user id
uinhistoriesis non-empty and contains only characters with'a' <= c <= 'z'or'0' <= c <= '9'