Count Mentions Per User Problem
Description
LeetCode Problem 3433.
You are given an integer numberOfUsers representing the total number of users and an array events of size n x 3.
Each events[i] can be either of the following two types:
- Message Event:
["MESSAGE", "timestamp_i", "mentions_string_i"]- This event indicates that a set of users was mentioned in a message at
timestamp_i. - The
mentions_string_istring can contain one of the following tokens:id<number>: where<number>is an integer in range[0,numberOfUsers - 1]. There can be multiple ids separated by a single whitespace and may contain duplicates. This can mention even the offline users.ALL: mentions all users.HERE: mentions all online users.
- This event indicates that a set of users was mentioned in a message at
- Offline Event:
["OFFLINE", "timestamp_i", "id_i"]- This event indicates that the user
id_ihad become offline attimestamp_ifor 60 time units. The user will automatically be online again at timetimestamp_i + 60.
- This event indicates that the user
Return an array mentions where mentions[i] represents the number of mentions the user with id i has across all MESSAGE events.
All users are initially online, and if a user goes offline or comes back online, their status change is processed before handling any message event that occurs at the same timestamp.
Note that a user can be mentioned multiple times in a single message event, and each mention should be counted separately.
Example 1:
Input: numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","71","HERE"]]
Output: [2,2]
Explanation:
Initially, all users are online.
At timestamp 10, id1 and id0 are mentioned. mentions = [1,1]
At timestamp 11, id0 goes offline.
At timestamp 71, id0 comes back online and "HERE" is mentioned. mentions = [2,2]
Example 2:
Input: numberOfUsers = 2, events = [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","12","ALL"]]
Output: [2,2]
Explanation:
Initially, all users are online.
At timestamp 10, id1 and id0 are mentioned. mentions = [1,1]
At timestamp 11, id0 goes offline.
At timestamp 12, "ALL" is mentioned. This includes offline users, so both id0 and id1 are mentioned. mentions = [2,2]
Example 3:
Input: numberOfUsers = 2, events = [["OFFLINE","10","0"],["MESSAGE","12","HERE"]]
Output: [0,1]
Explanation:
Initially, all users are online.
At timestamp 10, id0 goes offline.
At timestamp 12, "HERE" is mentioned. Because id0 is still offline, they will not be mentioned. mentions = [0,1]
Constraints:
1 <= numberOfUsers <= 1001 <= events.length <= 100events[i].length == 3events[i][0]will be one ofMESSAGEorOFFLINE.1 <= int(events[i][1]) <= 10^5- The number of
id<number>mentions in any"MESSAGE"event is between1and100. 0 <= <number> <= numberOfUsers - 1- It is guaranteed that the user id referenced in the
OFFLINEevent is online at the time the event occurs.
Solution
Approach: Sorting and Simulation
Sort the events by timestamp, with OFFLINE events before MESSAGE events at the same timestamp (an offline user is already offline for a message at that moment).
Then replay them. Keep onlineAt[u], the time user u comes back online, and a counter lazy for ALL mentions, which hit everybody and can be added at the end.
- OFFLINE: set
onlineAt[u] = timestamp + 60. - MESSAGE
ALL: increaselazy. - MESSAGE
HERE: add one to every user withonlineAt[u] <= timestamp. - MESSAGE with ids: add one to each mentioned user (repeated ids count repeatedly).
At the end, add lazy to every user.
Complexity: O(m log m + m * n + L) time and O(n) space, where n is the number of users, m the number of events and L the total length of the mention strings.
class Solution:
def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]:
events.sort(key=lambda e: (int(e[1]), e[0][2]))
ans = [0] * numberOfUsers
online_t = [0] * numberOfUsers
lazy = 0
for etype, ts, s in events:
cur = int(ts)
if etype[0] == "O":
online_t[int(s)] = cur + 60
elif s[0] == "A":
lazy += 1
elif s[0] == "H":
for i, t in enumerate(online_t):
if t <= cur:
ans[i] += 1
else:
for a in s.split():
ans[int(a[2:])] += 1
if lazy:
for i in range(numberOfUsers):
ans[i] += lazy
return ans