Efficient Solution | C++
IntuitionThis solution is designed to keep track of how many times each user is mentioned in a series of events. The events can either be MESSAGE events, where
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
This solution is designed to keep track of how many times each user is mentioned in a series of events. The events can either be MESSAGE events, where users are mentioned in messages, or OFFLINE events, where a user goes offline temporarily.
# How the solution works:
<!-- Describe your approach to solving the problem. -->

```
1. Sorting Events: We start by sorting all the events by timestamp so that we process them in the right order. If two events happen at the same time, we make sure to handle the MESSAGE events first. This is important because a message event could mention users who might go offline right after, so we need to handle the message before that.

2. Tracking User Status: We maintain an array that tracks whether each user is online or offline. Initially, all users are online, but whenever an OFFLINE event occurs, we mark the user as offline. Later, we use this to decide if a user should be counted as mentioned in a message.

3. Handling Mentions:

    - If a message mentions ALL users, all users (whether online or offline) are counted as mentioned.
    - If the message mentions HERE, only users who are currently online at the time of the message get counted.
    - If the message mentions specific users (like "id1 id0"), we count just those users as mentioned. We make sure to count every mention individually, so if a user is mentioned multiple times in a message, we count each mention.
4. Processing Offline Events: When a user goes offline, we add them to a priority queue to track when they’ll come back online. The user will be marked as online again after 60 time units. This helps us manage the timing correctly, ensuring users can’t be counted as online if they are offline at the moment of the message.

5. Final Adjustments: After processing all events, we add any mentions to ALL users to the final mention count for every user. This ensures that if a message mentioned everyone, those mentions get reflected for each user.
```

# Code
```cpp []
class Solution {
public:
    // Parses user IDs from the mention string (e.g., "id1 id0" -> [1, 0])
    vector<int> parseUserIds(string& str) {
        vector<int> result;
        stringstream ss(str);
        string id;
        while (ss >> id) {
            result.push_back(stoi(id.substr(2)));  // Extract user ID number
        }
        return result;
    }

    vector<int> countMentions(int numberOfUsers, vector<vector<string>>& events) {
        vector<int> mentions(numberOfUsers, 0);  // Store mentions count for each user
        vector<int> userStatus(numberOfUsers, 1);  // 1 = online, 0 = offline
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;  // To track offline users
        int allUsersMentioned = 0;  // For "ALL" mentions in messages

        // Sort events by timestamp, with MESSAGE events prioritized when timestamps are the same
        sort(events.begin(), events.end(), [](vector<string>& a, vector<string>& b) {
            return stoi(a[1]) < stoi(b[1]);
        });

        // Process each event
        for (int i = 0; i < events.size(); ++i) {
            if (events[i][0] == "MESSAGE") {
                // Process users coming online before this message event
                while (!pq.empty() && pq.top().first <= stoi(events[i][1])) {
                    userStatus[pq.top().second] = 1;  // Mark user as online
                    pq.pop();  // Remove user from the queue
                }

                if (events[i][2] == "ALL") {
                    allUsersMentioned++;  // Count mention for all users
                } else if (events[i][2] == "HERE") {
                    // Count mentions only for online users
                    for (int i = 0; i < userStatus.size(); ++i) {
                        if (userStatus[i] == 1) mentions[i]++;
                    }
                } else {
                    vector<int> mentionedUsers = parseUserIds(events[i][2]);
                    for (int id : mentionedUsers) {
                        mentions[id]++;  // Increment mention count for mentioned users
                    }
                }
            } else if (events[i][0] == "OFFLINE") {
                int userId = stoi(events[i][2]);
                pq.push({stoi(events[i][1]) + 60, userId});  // User goes offline for 60 time units
                userStatus[userId] = 0;  // Mark user as offline
            }
        }

        // Add the "ALL" mentions to all users
        for (int i = 0; i < mentions.size(); ++i) {
            mentions[i] += allUsersMentioned;
        }

        return mentions;
    }
};


```
['Array', 'Sorting', 'Simulation', 'C++']