JAVA || Easy Solution|| Beats 100% both space and time complexity|| Detailed Explanation|| Sorting
IntuitionWe are tasked with counting how many times each user is mentioned in the events. The events consist of either "OFFLINE" actions or "MESSAGE" actions. "
![image.png](https://assets.leetcode.com/users/images/ba3c0aba-f03e-4e4c-a6d7-95f51ceaa641_1737954764.2216294.png)

# Intuition
We are tasked with counting how many times each user is mentioned in the events. The events consist of either "OFFLINE" actions or "MESSAGE" actions. "OFFLINE" actions tell us when a user becomes offline, and "MESSAGE" actions give us mentions of users either individually, all users, or only the currently online users. The problem requires us to track user status (whether online or offline) and count the mentions based on these statuses.


# Approach
The approach involves sorting the events by their timestamp to process them in chronological order. Each event is either a user going "OFFLINE" or a message that may mention users. For each "OFFLINE" event, we mark the user as offline, updating their timestamp to indicate they were offline starting from that time. For each "MESSAGE" event, we will handle different types of mentions:

**"ALL" mentions all users, so we increment the count for every user.
"HERE" mentions all users who are currently online, so we increment the count for users who are online.**
Specific mentions (e.g., "user1", "user2") will update the count for the mentioned users if they are currently online.
**The timestamps help us to track when users go offline and ensure that we correctly count only mentions of users who are online at the time of the message.**

# Complexity
- Time complexity:
The main work involves sorting the events, which takes O(mlogm), where m is the number of events. Processing each event takes 
O(n) where n is the number of users, as we may iterate over all users when processing a "MESSAGE" event. Hence, the overall time complexity ***O(mlogm+m⋅n)***, where 
m is the number of events and n is the number of users.

- Space complexity:
We maintain two arrays: timestamp and mentions, both of size
n where n is the number of users. The space complexity is ***O(n)***.

# Code
```java []
import java.util.*;

class Solution {
    public int[] countMentions(int numberOfUsers, List<List<String>> events) {
        int[] timestamp = new int[numberOfUsers];
        int[] mentions = new int[numberOfUsers];

        events.sort((a, b) -> {
            int timeA = Integer.parseInt(a.get(1));
            int timeB = Integer.parseInt(b.get(1));
            if (timeA != timeB) return timeA - timeB;
            if (a.get(0).equals("OFFLINE")) return -1;
            if (b.get(0).equals("OFFLINE")) return 1;
            return 0;
        });

        for (List<String> event : events) {
            String type = event.get(0);
            int time = Integer.parseInt(event.get(1));

            if (type.equals("OFFLINE")) {
                int id = Integer.parseInt(event.get(2));
                timestamp[id] = -(time + 60);
            } else if (type.equals("MESSAGE")) {
                for (int i = 0; i < numberOfUsers; i++) {
                    if (timestamp[i] < 0 && -timestamp[i] <= time) {
                        timestamp[i] = 0;
                    }
                }

                String mentionsString = event.get(2);
                if (mentionsString.equals("ALL")) {
                    for(int i=0;i<numberOfUsers;i++)
                            mentions[i]++;
                } else if (mentionsString.equals("HERE")) {
                    for (int i = 0; i < numberOfUsers; i++) {
                        if (timestamp[i] >= 0) mentions[i]++;
                    }
                } else {
                    for (String idS : mentionsString.split(" ")) {
                        int id = Integer.parseInt(idS.substring(2));
                        mentions[id]++;
                    }
                }
            }
        }

        return mentions;
    }
}
```
['Java']