Count Mentions Per User
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

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

class Solution {
    public int[] countMentions(int numberOfUsers, List<List<String>> events) {
        List<Event> eventList = new ArrayList<>();
        for (int i = 0; i < events.size(); i++) {
            eventList.add(new Event(events.get(i), i));
        }

        Collections.sort(eventList, (a, b) -> {
            if (a.timeStamp != b.timeStamp) {
                return Integer.compare(a.timeStamp, b.timeStamp);
            }
            boolean aOffline = a.type.equals("OFFLINE");
            boolean bOffline = b.type.equals("OFFLINE");
            if (aOffline != bOffline) {
                return aOffline ? -1 : 1;
            }
            return Integer.compare(a.originalIndex, b.originalIndex);
        });

        int[] mentions = new int[numberOfUsers];
        int[] offlineUntil = new int[numberOfUsers];

        for (Event event : eventList) {
            String type = event.type;
            int timeStamp = event.timeStamp;
            List<String> data = event.data;

            if (type.equals("OFFLINE")) {
                int userId = Integer.parseInt(data.get(2));
                offlineUntil[userId] = timeStamp + 60; // Mark the time user goes offline
            } else if (type.equals("MESSAGE")) {
                String mentionStr = data.get(2);
                String[] tokens = mentionStr.split(" ");
                for (String token : tokens) {
                    if (token.equals("ALL")) {
                        for (int i = 0; i < numberOfUsers; i++) {
                            mentions[i]++;
                        }
                    } else if (token.equals("HERE")) {
                        for (int i = 0; i < numberOfUsers; i++) {
                            if (timeStamp >= offlineUntil[i]) { // User is online
                                mentions[i]++;
                            }
                        }
                    } else if (token.startsWith("id")) {
                        String numStr = token.substring(2);
                        int userId = Integer.parseInt(numStr);
                        if (userId >= 0 && userId < numberOfUsers) {
                            mentions[userId]++;
                        }
                    }
                }
            }
        }
        return mentions;
    }

    private static class Event {
        String type;
        int timeStamp;
        int originalIndex;
        List<String> data;

        public Event(List<String> data, int originalIndex) {
            this.type = data.get(0);
            this.timeStamp = Integer.parseInt(data.get(1));
            this.originalIndex = originalIndex;
            this.data = data;
        }
    }
}

```
['Java']