You are rendering a streaming app home page.
titleId
values (integers).
Implement a function that filters titles while preserving order under the following deduplication rules:
X output titles
for a shelf, you must
skip
any
titleId
that has already appeared in the
visible region
(first
X
output titles) of any earlier shelf.
X
output titles, continue outputting additional titles from that shelf, but now
only dedupe within the same shelf
(i.e., skip repeats that have already been output in this shelf).
X
within a shelf.
shelves: List[List[int]]
,
X: int
List[List[int]]
(the filtered shelves)
X
eligible titles (after global dedupe), it may output fewer than
X
titles.
If X = 2 and shelves are:
[1, 2, 1, 3]
[2, 4, 2, 5]
Then:
[1, 2]
(global seen =
{1,2}
), then locally can output
3
(skipping the repeated
1
).
2
(already globally visible), so it outputs
[4, 2]
as its first 2 outputs (4 is new globally visible; once the shelf has hit
X
outputs,
2
is allowed even though it was globally visible). After that it can output
5
.
So the output is [[1, 2, 3], [4, 2, 5]].
Specify and implement the algorithm.