Count mentions per user easy solution intuitive approach using hash map
IntuitionThe intuition was to check offline messages and create range for each user when it was online or offline and I chose online periods then go through all
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The intuition was to check offline messages and create range for each user when it was online or offline and I chose online periods then go through all messages and according to given condition add mention count of users in the mentions array and return the array.
# Approach
<!-- Describe your approach to solving the problem. -->
The approach was to sort the offline messages according to timestamp so we get them in chronological order and have no issues in creating onlinePeriods like [0,5],[65,100].Then once we have our online periods we will go through each messages and check if the mentioned user was online on the timestamp and increase the count of its mention in mentions array by the number of times it is mentioned.For "all" we will directly increase or all users by 1. For "here" we will go through the map and check if any user in that time period is online if yes then we will increase it by 1 and we will move forward to other users.
This was the approach to solve this questions the data structure used were map and vector to do so.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
The total time complexity combines the time for processing offline and message events:
O(E.(N+logE))
Where:
E is the number of events,
N is the number of users,
logE comes from sorting the events.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
The total space complexity is dominated by the storage of event data, user online periods, and mentions:
O(E.N)
# Code
```cpp []
class Solution {
public:
    vector<int> countMentions(int numberOfUsers, vector<vector<string>>& events) {
       vector<int> mentions(numberOfUsers, 0); // Track mentions for each user
unordered_map<int, vector<pair<long long, long long>>> userOnlinePeriods; // Track online periods for each user

for (int i = 0; i < numberOfUsers; ++i) {
    userOnlinePeriods[i].push_back({0, LLONG_MAX}); // Initially all users are online
}

// Process all OFFLINE events first and sort by timestamp
vector<vector<string>> offlineEvents;
for (auto &event : events) {
    if (event[0] == "OFFLINE") {
        offlineEvents.push_back(event);
    }
}
sort(offlineEvents.begin(), offlineEvents.end(), [](const vector<string> &a, const vector<string> &b) {
    return stoll(a[1]) < stoll(b[1]);
});

for (auto &event : offlineEvents) {
    long long timestamp = stoll(event[1]);
    int userId = stoi(event[2]);
    userOnlinePeriods[userId].back().second = timestamp; // End the current online period
    userOnlinePeriods[userId].push_back({timestamp + 60, LLONG_MAX}); // Start a new online period
}

// Process MESSAGE events
for (auto &event : events) {
    string type = event[0];
    if (type != "MESSAGE") continue;

    long long timestamp = stoll(event[1]);
    string mentionsString = event[2];
    unordered_map<int, int> mentionedUsersCount; // Track how many times each user is mentioned

    if (mentionsString == "ALL") {
        for (int i = 0; i < numberOfUsers; ++i) {
            mentionedUsersCount[i] = 1;
        }
    } else if (mentionsString == "HERE") {
        for (int i = 0; i < numberOfUsers; ++i) {
            // Check if the user is online at the timestamp
            bool isOnline = false;
            for (auto &[start, end] : userOnlinePeriods[i]) {
                if (start <= timestamp && timestamp < end) {
                    isOnline = true;
                    break;
                }
            }
            if (isOnline) {
                mentionedUsersCount[i] = 1;
            }
        }
    } else {
        stringstream ss(mentionsString);
        string token;
        while (ss >> token) {
            if (token.substr(0, 2) == "id") {
                int userId = stoi(token.substr(2));
                mentionedUsersCount[userId]++;
            }
        }
    }

    // Count mentions for all users in the map
    for (auto &[userId, count] : mentionedUsersCount) {
        mentions[userId] += count;
    }
}

return mentions;

    }
};
```
['Array', 'Hash Table', 'C++']