Sorting | Simulation | C++
IntuitionThe problem involves sorting events based on timestamps and processing specific instructions to count mentions for each user. The key insight is to use
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves sorting events based on timestamps and processing specific instructions to count mentions for each user. The key insight is to use a custom comparator for sorting and efficiently handle the events in the order they occur. We maintain a state vector to track the status of each user and update counts accordingly.



# Approach
<!-- Describe your approach to solving the problem. -->
1. Sorting the Events:
The events are sorted using a custom comparator.
Events are primarily sorted by their timestamps (numeric value of v1[1]), and if timestamps are equal, "OFFLINE" events are prioritized.
2. Processing the Events:
Iterate through each event in the sorted order.
Handle different types of events:
- For "MESSAGE" events:
If the target is "HERE", increment the count for users who are active (not offline in the last 60 seconds).
If the target is "ALL", increment the count for all users.
If the target specifies individual users, parse the target and update their counts.
- For "OFFLINE" events:
Update the timestamp for the respective user to mark when they went offline.
Maintain a vector v to track the last active timestamp for each user and another vector ans to store the counts.
3. Return the Result:
After processing all events, return the ans vector containing the counts for each user.

# Complexity
- Time complexity: **$$O(m⋅max(n,k))$$**
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Sorting the events: O(m⋅log(m)), where 𝑚 is the number of events.
Processing events: O(m⋅max(n,k)), where 𝑘 is the total size of all individual user mentions.

- Space complexity: **$$O(n+m)$$**
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
The v and ans vectors each take O(n) space, where 𝑛 is the number of users.


# Code
```cpp []
class Solution {
public:
struct Compare{
    bool operator()(vector<string>&v1, vector<string>&v2){
        if(v1[1]!=v2[1]) return stoi(v1[1])<stoi(v2[1]);
        return v1[0]=="OFFLINE";
    }
};
    vector<int> countMentions(int n, vector<vector<string>>& e) {
        sort(e.begin(), e.end(), Compare());
        vector<int>v(n, -1), ans(n, 0);
        int j;
        string t;
        for(auto i:e){
            // cout<<i[0]<<" "<<i[1]<<" "<<i[2]<<endl;
            if(i[0]=="MESSAGE"){
                if(i[2]=="HERE"){
                    for(int j=0;j<ans.size();j++){
                        // cout<<v[j]<<" "<<stoi(i[1])<<endl;
                        if(v[j]==-1 || v[j]<=stoi(i[1])-60) ans[j]++;
                    }
                }else if(i[2]=="ALL"){
                    for(int j=0;j<ans.size();j++) ans[j]++;
                }else{
                    j=0;
                    t="";
                    while(j<i[2].size()){
                        if(i[2][j]=='i' || i[2][j]=='d') j++;
                        else if(i[2][j]==' ') ans[stoi(t)]++, t="", j++;
                        else{
                            t+=i[2][j];
                            j++;
                        }
                    }
                    ans[stoi(t)]++;
                }
            }else v[stoi(i[2])]=stoi(i[1]);
        }
        return ans;
    }
};
```
['Array', 'Sorting', 'Simulation', 'C++']