🚀 Easy | Weekly Contest 434 | Javascript Solution
Approach / Intuition

Sorting Events by Timestamp and Priority:

The events array is sorted primarily by timestamp (ascending) and secondarily by the type of ev
## Approach / Intuition

1. **Sorting Events by Timestamp and Priority**:  
   - The `events` array is sorted primarily by timestamp (ascending) and secondarily by the type of event (`"OFFLINE"` events take precedence when timestamps are the same).
   - This ensures that events are processed in the correct chronological order, and offline states are updated promptly.

2. **Initialize State Tracking**:  
   - `mentions`: An array to keep track of the count of mentions for each user, initialized to zero.
   - `offlineUsers`: An array to track the timestamp when each user went offline, initialized to `-1` to indicate all users are initially online.

3. **Handle User Offline States**:  
   - Before processing an event, the algorithm checks the `offlineUsers` array to reset any user who has been offline for more than 60 seconds. This ensures only recent offline statuses are respected.

4. **Process Each Event**:  
   - If the event is `"OFFLINE"`, the user is marked as offline at the given timestamp.
   - For other events:
     - `"ALL"`: All users' mention counts are incremented.
     - `"HERE"`: Only online users' mention counts are incremented.
     - Custom user mentions (e.g., `"id1 id2"`): Parse the message and increment the mention count for the specified users.

5. **Return the Results**:  
   - After processing all events, the `mentions` array, which now contains the total mentions for each user, is returned as the output. This represents the cumulative effect of all processed events.

⬆️ Upvote, if you understand the approach.


## Complexity
- Time complexity: O(E logE) + O(E.N).
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

## Code
```javascript []
/**
 * @param {number} numberOfUsers
 * @param {string[][]} events
 * @return {number[]}
 */
var countMentions = function(numberOfUsers, events) {

    // Sort events by timestamp. If timestamps are equal, prioritize "OFFLINE" events.
    events = events.sort((a, b) => {
        const num1 = parseInt(a[1]), num2 = parseInt(b[1]);
        if (num1 == num2) {
            if (a[0] == "OFFLINE") {
                return -1;
            } else {
                return 1;
            }
        } else {
            return num1 - num2;
        }
    });

    const mentions = Array(numberOfUsers).fill(0); // Tracks mention count for each user.
    const offlineUsers = Array(numberOfUsers).fill(-1); // Tracks offline timestamps for each user (-1 means online).

    for (const [message, timestamp, messageString] of events) {
        // Check if any user has been offline for 60+ seconds; mark them as online if so.
        for (let i = 0; i < numberOfUsers; i++) {
            if (offlineUsers[i] != -1 && timestamp - offlineUsers[i] >= 60) {
                offlineUsers[i] = -1; // Reset user to available.
            }
        }

        if (message == "OFFLINE") {
            // Mark the user as offline with the current timestamp.
            offlineUsers[messageString] = parseInt(timestamp);
            continue;
        } else {
            // Handle mentions based on the message type.
            switch (messageString) {
                case "ALL": {
                    // Increment mentions for all users.
                    for (let i = 0; i < numberOfUsers; i++) {
                        mentions[i]++;
                    }
                    break;
                }
                case "HERE": {
                    // Increment mentions only for online users.
                    for (let i = 0; i < numberOfUsers; i++) {
                        if (offlineUsers[i] == -1) {
                            mentions[i]++;
                        }
                    }
                    break;
                }
                default: {
                    // Parse specific mentions (e.g., "id1 id2") and increment counts for mentioned users.
                    const mentionedUsers = messageString.replace(/id/mg, '').split(' ');
                    for (const user of mentionedUsers) {
                        mentions[user]++;
                    }
                    break;
                }
            }
        }
    }
    return mentions;
};
```
['Array', 'Sorting', 'JavaScript']