Active Users in the Last Five Minutes
Problem statement
An analytics component reads a stream of log events in time order. Event i happened at second timestamps[i] and was made by user userIds[i].
After each event, report how many different users are active: a user is active if at least one of their events so far happened less than 300 seconds (5 minutes) before the current event. In other words, an earlier event at second s counts while timestamps[i] - s < 300; an event exactly 300 seconds old no longer counts. The current event always counts.
Only events up to and including the current one are known; later events in the same second are not counted yet. Return one count per event, in order.
Function
activeUsers(timestamps: int[], userIds: int[]) → int[]Examples
Example 1
timestamps = [0,100,250,299,300,650]userIds = [7,8,7,9,8,7]return = [1,2,2,3,3,1]At time 299 the events at 0, 100, 250 and 299 are all less than 300 seconds old, so the users are 7, 8 and 9: 3. At time 300 the event at 0 is exactly 300 seconds old and has left the window; users 8, 7, 9 and 8 remain, so the count is still 3. At time 650 only the current event is in the window: 1.
Example 2
timestamps = [5,5,5]userIds = [1,1,2]return = [1,1,2]All three events happen in the same second. After the second event user 1 is still the only user, and after the third the count is 2.
Example 3
timestamps = [42]userIds = [1000000]return = [1]One event, one active user.
Constraints
1 <= timestamps.length <= 3000anduserIds.length == timestamps.length0 <= timestamps[i] <= 10^6timestampsnever decreases (several events can share a second).1 <= userIds[i] <= 10^6