I simulate events in increasing timestamp order and, for events sharing a timestamp, I process all OFFLINE events before MESSAGE events (because status changes are applied before messages at the same time). I keep an offline_until time for each user (the time they become online again); a user is online at time T iff T >= offline_until[user]. On an OFFLINE event at time t for user id I set offline_until[id] = t + 60. On a MESSAGE event at time t I parse the mentions string token-by-token:
- ALL increments every user's count (offline or online).
- HERE increments the count for every user currently online at time t.
- idX increments user X (count each occurrence; duplicates count separately).
Finally I return the mention counts. This runs fast because numberOfUsers ≤ 100 and events ≤ 100.
class Solution:
def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]:
proc = []
for typ, ts_str, arg in events:
t = int(ts_str)
order = 0 if typ == "OFFLINE" else 1
proc.append((t, order, typ, arg))
proc.sort()
mentions = [0] * numberOfUsers
offline_until = [0] * numberOfUsers
for t, _, typ, arg in proc:
if typ == "OFFLINE":
uid = int(arg)
offline_until[uid] = t + 60
else:
tokens = arg.split()
for token in tokens:
if token == "ALL":
for u in range(numberOfUsers):
mentions[u] += 1
elif token == "HERE":
for u in range(numberOfUsers):
if t >= offline_until[u]:
mentions[u] += 1
else:
if token.startswith("id"):
uid = int(token[2:])
mentions[uid] += 1
else:
pass
return mentions- Time: O(E * U)
- Space: O(U)