Beats 100% | Sorting | Solution for LeetCode#3433
IntuitionThe problem involves tracking mentions and offline status for users in a messaging system. We need to process events chronologically and update user me
![image.png](https://assets.leetcode.com/users/images/83df1f1a-3fb0-4833-bcc0-e7eb2cdc17bb_1737866575.5712762.png)

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves tracking mentions and offline status for users in a messaging system. We need to process events chronologically and update user mentions based on different types of messages and offline statuses.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Sort the events based on timestamp, with MESSAGE events taking precedence over OFFLINE events if timestamps are equal.
2. Process events sequentially:
	- For MESSAGE events, update mentions based on the message content (ALL, HERE, or specific user mentions).
	- For OFFLINE events, update the offline time for specified users.
3. Handle the "HERE" mention by checking if users are online or have been offline for less than 60 minutes.

# Complexity
- Time complexity: O(n log n + m)
n log n for sorting events
m for processing events (m is the total number of words in all messages)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# Code
```java []
class Solution {
    public int[] countMentions(int numberOfUsers, List<List<String>> events) {
        int[] mentions=new int[numberOfUsers];
        int[] offTime=new int[numberOfUsers];

        Collections.sort(events, (a,b)->Integer.parseInt(a.get(1))==Integer.parseInt(b.get(1))? b.get(0).compareTo(a.get(0)): Integer.parseInt(a.get(1))-Integer.parseInt(b.get(1))
                        );
        
        for(int i=0; i<events.size(); i++){
            if(events.get(i).get(0).equals("MESSAGE")){
                messageFunc(events.get(i), mentions, offTime);
            }
            else if(events.get(i).get(0).equals("OFFLINE")){
                offlineFunc(events.get(i), mentions, offTime);
            }
        }

        return mentions;
    }

    void messageFunc(List<String> event, int[] mentions, int[] offTime){
        int time=Integer.parseInt(event.get(1));
        String[] str=event.get(2).split(" ");

        for(String s:str){
            if(s.equals("ALL")){
                for(int i=0; i<mentions.length; i++){
                    mentions[i]+=1;
                }
            }
            else if(s.equals("HERE")){
                for(int i=0; i<mentions.length; i++){
                    if(offTime[i]==0){ //Only online
                        mentions[i]+=1;
                    }
                    else if(offTime[i]+60<=time){
                        mentions[i]+=1;
                        offTime[i]=0;
                    }
                }
            }
            else{
                int idx=Integer.parseInt( s.substring("id".length()) );
                mentions[idx]+=1;
            }
        }
    }

    void offlineFunc(List<String> event, int[] mentions, int[] offTime){
        int time=Integer.parseInt(event.get(1));
        String[] str=event.get(2).split(" ");
        
        for(String s:str){
            int idx=Integer.parseInt(s);
            offTime[idx]=time;
        }
    }
}
```
['Java']