🔥 FAST 🔥 | 31 ms | Beats 100% | Python3 Simple Real-World Approach Using Dual Arrays + Sorting
IntuitionWe need to track when each user is online or offline and then count every mention across messages. By carefully processing when users go offline and co
# Intuition
We need to track when each user is online or offline and then count every mention across messages. By carefully processing when users go offline and come back online, we ensure we only include those users in “HERE” mentions if they are actually online at the time of the message. The "gotcha" in this problem is making sure to process the events in the correct order.

# Approach  
First, we sort all events by timestamp, ensuring offline events are processed before message events at the same timestamp. We keep an `online` array that stores the earliest timestamp when each user is back online (initially `0` for always-online at the start). For each “OFFLINE” event, we mark the user offline for 60 time units. For each “MESSAGE” event, we decide who is mentioned based on whether it is “ALL”, “HERE” (everyone whose `online[i]` is <= message timestamp), or a list of explicit ids. Finally, we increment each mentioned user’s count accordingly.

# Complexity
### Time complexity
Sorting the events costs $$O(m*log(m))$$, where $$m$$ is the number of events. Processing each event involves either updating one user (`OFFLINE`) or iterating over a range of users (`MESSAGE`). Worst case, each message might mention all users, leading to a potential $$O(m * n)$$ in the worst scenario where $$n$$ is the number of users, making the overall complexity $$O(m*log(m) + m * n)$$.

### Space complexity
$$O(n)$$ to store the `online` and `mentions` arrays

<img src="https://assets.leetcode.com/users/images/91b06084-fcb9-48e5-8e2f-581a4ee5d146_1737932513.3988116.png" style="width: 300px"/>

#### _Like if you found this solution helpful!_


# Code
```python3 []
class Solution:
    def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]:
        n = numberOfUsers

        # simple count of mentions per user
        mentions = [0] * n

        # next timestamp the user is online. if online[id] <= message_time, then user is online
        online = [0] * n

        # sort the events by timestamp to ensure they come in the correct order,
        # and then OFFLINE events come before MESSAGE
        events = sorted(events, key=lambda x: (int(x[1]), 0 if x[0] == "OFFLINE" else 1))

        for event in events:
            if event[0] == "OFFLINE":
                id = int(event[2])
                # set the user to go back online in 60 time units
                online[id] = int(event[1]) + 60

            elif event[0] == "MESSAGE":
                # timestamp of the event                
                ts = int(event[1])

                if event[2] == "ALL":
                    # array of everyone
                    ids = range(n)
                elif event[2] == "HERE":
                    # array of everyone online at timestamp ts
                    ids = [i for i in range(n) if online[i] <= ts]
                else:
                    # array of the specific users mentioned
                    ids = [int(id[2:]) for id in event[2].split()]

                for id in ids:
                    mentions[id] += 1

        return mentions
```


<img src="https://assets.leetcode.com/users/images/d18cdcca-fd62-4384-9992-628e819bda86_1737933391.3370376.png" style="width:300px"/>

['Array', 'Sorting', 'Python3']