Detailed Explanation using Sorting only | Easy to Understand | C++
IntuitionWe have to go through the events according to their timeStamp i.e. perform events with less timeStamp before the events with more timeStamp, so that we
# Intuition
We have to go through the events according to their timeStamp i.e. perform events with less timeStamp before the events with more timeStamp, so that we can keep track of the users who are offline at the time of current event.
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
- Sort the Events according to their timeStamp and give preference to OFFLINE event if two events have same timeStamp, because it is mentioned in the question description that "if a user goes offline or comes back online, their status change is processed before handling any message event that occurs at the same timestamp."
- Declare arrays $$offlineTime$$ and $$mentions$$ of size $$numberOfUsers$$, where $$offlineTime[i]$$= time upto which i'th user will be offline (excluding this time) and $$mentions[i]$$= number of times i'th user is mentioned.
- Now go through each event e and update the $$mentions$$ of each user accordingly.
- Return the $$mentions$$ array.

# Complexity
- Time complexity:
$$O(M*log(M) + M*N)$$
where M = number of Events and N = number of Users.
M*log(M) - for Sorting Events.
In worst case,for each event e we can traverse all the users u so time complexity for this will be O(M*N).

- Space complexity:
$$O(N)$$ , where N = Number of Users.



# Code
```cpp []
class Solution {
public:
    vector<int> countMentions(int numberOfUsers, vector<vector<string>>& events) {

        sort(events.begin(), events.end(), [] (auto e1, auto e2) { 
            if(stoi(e1[1]) == stoi(e2[1]))
            return e1[0][0] > e2[0][0];   //if same timeStamp then put offline event first i.e. return true if e1[0][0]=='o' (offline)
            
            return stoi(e1[1]) < stoi(e2[1]);   // else put less timeStamp first
             
        }); 
        
        //for every event go through all users and update their mentions TC= events_Size*numberOfUsers
        vector<int> offlineTime(numberOfUsers, 0);  // user -> end of offline time i.e. time upto which i'th user is offline
        vector<int> mentions(numberOfUsers, 0);   // store number of times a user is mentioned          
        
        for(int i = 0; i < events.size(); i++)
        {  
            string eventName = events[i][0];
            int timeStamp = stoi(events[i][1]);
            string mentionedIds = events[i][2];
            
            if(eventName == "MESSAGE")
            {   
                if(mentionedIds == "ALL")  //mention all users
                {      
                    for(int i = 0; i < numberOfUsers; i++)
                        mentions[i]++;
                }
                else if(mentionedIds == "HERE")  //mention all online users
                {   
                    for(int i = 0; i < numberOfUsers; i++)
                    {
                       //if user is online(i.e. not offline) then count its mention
                       // at time 'timeStamp + 60' id will be online so include this time as well
                        if(offlineTime[i] <= timeStamp)   
                            mentions[i]++;
                    }
                }
                else
                {   
                    //mention all given ids
                    for(int i = 0; i < mentionedIds.size(); i++)
                    {
                        if(mentionedIds[i] == 'd')
                        {
                            i++;  // index next to 'd' contains id number
                            string id = "";  // id can be of >1 digits so get all the digits
                            while(i < mentionedIds.size() && mentionedIds[i] != ' ')
                            {
                                id += mentionedIds[i];
                                i++;
                            }

                            mentions[stoi(id)]++;
                        }
                    }
                }
            }
            
            //if its "OFFLINE" event
            else  
            {  
                int id = stoi(mentionedIds); // get user no. to be set offline

                //set the time upto which this user will be offline
                offlineTime[id] = timeStamp + 60; // at time 'timeStamp + 60' id will be online
            }
        }
        return mentions;
    }   

};

```
If you like my solution even 0.001% then Please Please Please UPVOTE/LIKE my Solution.
['Hash Table', 'Sorting', 'C++']