Contest 434 || Question-2
Approach & Intuition:The problem appears to involve tracking user mentions based on event logs, where users go offline for some duration and mentions are counte
### **Approach & Intuition:**
The problem appears to involve tracking user mentions based on **event logs**, where users go **offline** for some duration and mentions are counted based on different conditions.

---

### **Step-by-step Explanation:**
1. **Sorting the Events List**  
   - The `events` list is sorted by **timestamp** (`list.get(1)`), ensuring events are processed in chronological order.  
   - If two events have the same timestamp, they are sorted lexicographically in **descending order** based on `list.get(0)` (event type).  
   - Sorting ensures that `OFFLINE` events are processed before `MENTION` events at the same timestamp.

2. **Processing Events**  
   - We maintain an `online` array (`online[j]` stores the **timestamp** until which user `j` is offline).  
   - We initialize `online[j] = -1`, meaning all users start as **online**.

3. **Iterating through Events:**
   - **If event type is "OFFLINE"**  
     - Marks specific users as offline for the next **60 minutes** (`time + 60`).  
   - **If event type is "MENTION"**  
     - **"ALL"**: Increments mention count for all users.  
     - **"HERE"**: Increments mention count only for users who are currently online.  
     - **Specific mentions ("ID_1 ID_2 ...")**: Increments count for specified user IDs.

---

### **Time Complexity Analysis:**
- **Sorting the list**: \(O(M*log M)\), where \(M\) is the number of events.  
- **Iterating through events**: \(O(M * n)\) in the worst case (each mention event loops through all `n` users).  
- **Final Complexity**:  
  - **Best case (few events, small `n`)**: \(O(Mlog M + M)\)  
  - **Worst case (large `n` and mentions affecting all users)**: \(O(Mlog M + M * n)\)  

---

### **Possible Optimizations:**
1. **Use a HashMap instead of an array (`online[]`)**  
   - Instead of checking all `n` users, maintain an `offlineMap` storing only users who are currently offline.  
   - This reduces the **per-event iteration** from `O(n)` to **only affected users**.
   
2. **Use a TreeMap for Efficient Range Queries**  
   - Instead of checking each user's online status, a **TreeMap (timestamp → set of users)** can efficiently track offline durations.

---

# Code
```java []
class Solution{
    public int[] countMentions(int n, List<List<String>> events) {
         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))
                        );
        int[] answer = new int[n];
        int[] online = new int[n];
        Arrays.fill(online,-1);
        for(int i=0;i<events.size();i++){

            List<String> list = events.get(i);
            int time = Integer.valueOf(list.get(1));
            for(int j=0;j<n;j++){
                if(online[j]<=time){
                    online[j] = -1;
                }
            }
            if(list.get(0).equals("OFFLINE")){
                String s = list.get(2)+" ";
                String[] str = s.split(" ");
                for(int j=0;j<str.length;j++){
                    online[Integer.valueOf(str[j])] = time+60;
                }
            }
            else{
                 
                 if(list.get(2).equals("ALL")){
                    for(int j=0;j<n;j++) answer[j]++;
                 }
                 else if(list.get(2).equals("HERE")){
                    for(int j=0;j<n;j++){
                        if(online[j]==-1){
                            answer[j]++;
                        }
                    }
                 }
                 else{
                     String s = list.get(2)+" ";
                     String[] str = s.split(" ");
                for(int j=0;j<str.length;j++){
                    answer[Integer.valueOf(str[j].substring(2))]++;
                }

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