Compact Conversations by Unique Listings
Problem statement
You are given an ordered array conversations. Each conversation contains listing identifiers, and an identifier may appear in several conversations or more than once in one conversation.
While the retained history contains more than maxUniqueListings distinct identifiers, remove the oldest entire conversation. Use identifier occurrence counts so an identifier stops contributing to the distinct total only after its final retained occurrence is removed.
After eviction finishes, return the distinct identifiers from the retained conversations in first-appearance order. Include each identifier exactly once.
Function
compactUniqueListings(conversations: String[][], maxUniqueListings: int) → String[]Examples
Example 1
conversations = [["a","b","c"],["b","c","d"],["e"]]maxUniqueListings = 4return = ["b","c","d","e"]All conversations contain five distinct identifiers. Removing the oldest conversation deletes the final occurrence of "a", leaving the first-appearance order ["b","c","d","e"].
Example 2
conversations = [["x","x"],["x"]]maxUniqueListings = 1return = ["x"]The history already contains one distinct identifier, so no conversation is removed. Repeated occurrences produce one output entry.
Example 3
conversations = [["a"],["b"],["a"]]maxUniqueListings = 1return = ["a"]Removing the first conversation does not remove "a" from the distinct set because it appears later. Removing the second conversation deletes "b", leaving only "a".
Constraints
0 <= conversations.length <= 10^5- The total number of listing occurrences across all conversations is at most
10^5. - Every listing identifier is a non-empty ASCII string.
0 <= maxUniqueListings <= 10^5