Loading...
Loading...
You are given a stream of events. Each event goes from a source to a destination, and both are integer ids. The window holds only the most recent windowSize events.
Implement the class SourceRanker:
SourceRanker(windowSize, topCount) creates a ranker with no events.record(source, destination) adds an event from source to destination. If the window already holds windowSize events, the oldest one leaves it.top() returns up to topCount source ids, ranked by score.The score of a source is the number of distinct destinations among its events in the window. A destination recorded several times by the same source counts once. It keeps counting until the last of those events leaves the window.
top() ranks only the sources with at least one event in the window. Higher scores come first. Sources with equal scores are ordered by id, smallest first. Return the first topCount sources of that ranking. If fewer sources have events in the window, return all of them. If the window is empty, return [].
Input: operations = ["SourceRanker","record","record","record","top","record","record","top"], arguments = [[4,2],[1,10],[1,20],[2,10],[],[2,30],[2,40],[]]
Output: [null,null,null,null,[1,2],null,null,[2,1]]
Explanation: The window holds the last 4 events. After three events, source 1 has destinations 10 and 20 (score 2) and source 2 has destination 10 (score 1), so top() returns [1,2].
Two more events make five, so the first event (1, 10) leaves the window. Source 1 now has only destination 20 (score 1). Source 2 has 10, 30, and 40 (score 3), so top() returns [2,1].
Input: operations = ["SourceRanker","record","record","record","top","record","top","record","top"], arguments = [[3,3],[5,7],[5,7],[3,8],[],[3,9],[],[4,1],[]]
Output: [null,null,null,null,[3,5],null,[3,5],null,[3,4]]
Explanation: Source 5 records destination 7 twice, which counts once. Sources 3 and 5 both have score 1, so the smaller id comes first: [3,5].
After (3, 9), the first (5, 7) leaves the window. The second (5, 7) is still in it, so source 5 keeps score 1, and source 3 has score 2.
After (4, 1), the second (5, 7) leaves too. Source 5 has no events in the window, so it is not ranked at all: [3,4].
Input: operations = ["SourceRanker","top","record","record","top","record","top"], arguments = [[3,1],[],[9,1],[0,1],[],[9,2],[]]
Output: [null,[],null,null,[0],null,[9]]
Explanation: The window is empty at first, so top() returns [].
Sources 9 and 0 then both have score 1. The tie goes to the smaller id, and topCount is 1, so top() returns [0].
After (9, 2), source 9 has score 2 and ranks first, even though its id is larger.
windowSize ≤105topCount ≤10source, destination ≤109record and top calls ≤105