Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created December 12, 2025 22:57
Show Gist options
  • Select an option

  • Save Ifihan/b60e47d5ba39501bdde3c10790408fe7 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/b60e47d5ba39501bdde3c10790408fe7 to your computer and use it in GitHub Desktop.
Count Mentions Per User

Question

Approach

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.

Implementation

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

Complexities

  • Time: O(E * U)
  • Space: O(U)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment