Sort and simulate [Java]
ApproachSort the array and process the event one by one.Complexity

Time complexity:
O(nlogn)


Space complexity:
O(n)

Code
# Approach
Sort the array and process the event one by one.

# Complexity
- Time complexity:
$$O(nlogn)$$

- Space complexity:
$$O(n)$$

# Code
```java []
class Solution {
    private static final int TIMEOUT = 60;

    private int parseIds(String target, int[] idBuffer) {
        int j = 0;
        int num = 0;
        for (int i = 0;i < target.length(); ++i) {
            char ch = target.charAt(i);
            if (ch >= '0' && ch <= '9') {
                num = num * 10 + (ch - '0');
            } else if (ch == ' ') {
                idBuffer[j++] = num;
                num = 0;
            }
        }
        idBuffer[j++] = num;
        return j;
    }

    public int[] countMentions(int numberOfUsers, List<List<String>> events) {
        Collections.sort(events, (List<String> a, List<String> b) -> {
            int res = Integer.compare(
                Integer.parseInt(a.get(1)), 
                Integer.parseInt(b.get(1))
            );
            return res == 0 ? Integer.compare(
                b.get(0).charAt(0),
                a.get(0).charAt(0)
            ) : res;
        });
        int[] users = new int[numberOfUsers];
        int[] idBuffer = new int[numberOfUsers];
        int[] offlines = new int[numberOfUsers];

        for (List<String> event: events) {
            final char msg = event.get(0).charAt(0);
            if (msg == 'M') { // MESSAGE
                final String target = event.get(2);
                final char targetFirst = target.charAt(0);
                if (targetFirst == 'A') { // ALL
                    Arrays.setAll(users, i -> users[i] + 1);
                } else if (targetFirst == 'H') { // HERE
                    final int timestamp = Integer.parseInt(event.get(1));
                    for (int i = 0;i < users.length; ++i) {
                        if (offlines[i] <= timestamp) {
                            ++users[i];
                        }
                    }
                } else { //id1 id2
                    int numOfIds = parseIds(target, idBuffer);
                    for (int i = 0; i < numOfIds; ++i) {
                        ++users[idBuffer[i]];
                    }
                }
            } else if (msg == 'O') { // OFFLINE
                final int timestamp = Integer.parseInt(event.get(1));
                final int id = Integer.parseInt(event.get(2));
                offlines[id] = timestamp + TIMEOUT;
            }
        }
        return users;
    }
}
```
['Java']