Online Users by Array | Clean Code
IntuitionUse an array onlineTime to keep tack of online users by online time.
Note: Need to sort events.Complexity
Time complexity:
Sorting: O(eloge), e: events
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Use an array `onlineTime` to keep tack of online users by online time.
Note: Need to sort `events`.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Sorting: $$O(e \log e)$$, $$e$$: `events.length`
Processing: $$O(e * n)$$, $$n$$: `numberOfUsers`

- 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) {
        Collections.sort(events, (a, b) -> {
            if(!a.get(1).equals(b.get(1))){
                return Integer.parseInt(a.get(1)) - Integer.parseInt(b.get(1));
            }
            else{
                if(a.get(0).equals(b.get(0))){
                    return -1; // not changed
                }
                else if(a.get(0).equals("OFFLINE")){
                    return -1; // a, b
                }
                else{
                    return 1; // b, a
                }
            }
        });
        
        int[] res = new int[numberOfUsers];
        int[] onlineTime = new int[numberOfUsers];
        for(List<String> e: events){
            int currTime = Integer.parseInt(e.get(1));
            
            if(e.get(0).equals("OFFLINE")){
                int id = Integer.parseInt(e.get(2));
                int wakeTime = Integer.parseInt(e.get(1)) + 60;
                onlineTime[id] = wakeTime;
            }
            else{ // "MESSAGE"
                if(e.get(2).equals("ALL")){
                    for(int id = 0; id < numberOfUsers; id++){
                        res[id]++;
                    }
                }
                else if(e.get(2).equals("HERE")){
                    for(int id = 0; id < numberOfUsers; id++){
                        if(onlineTime[id] <= currTime){
                            res[id]++;
                        }
                    }
                }
                else{
                    String[] parts = e.get(2).split(" ");
                    for(String idx: parts){
                        int id = Integer.parseInt(idx.substring(2));
                        res[id]++;
                    }
                }
            }
        }
        return res;
    }
}
```
['Java']