Smart offline tracking for efficient mention counting in time-ordered events.
IntuitionWe need to track online status of users and process different types of events:
Online events: Update a user's online status
All mentions: Increment men
# Intuition
We need to track online status of users and process different types of events:
- Online events: Update a user's online status
- All mentions: Increment mention count for all users
- Here mentions: Increment mention count for all offline users
- Direct mentions: Increment mention count for specifically mentioned users


# Approach
1. Convert timestamps to integers and sort events by time
2. For events with the same timestamp, prioritize OFFLINE events before messages
3. Track the last offline time for each user
4. Process each event based on its type:
   - OFFLINE: Update the user's offline timestamp
   - MESSAGE/ALL: Increment count for all users
   - MESSAGE/HERE: Increment count for users offline for at least 60 time units
   - Direct mentions: Parse @u{id} format and increment specific users

# Complexity
- Time complexity: O(n log n) where n is the number of events (dominated by sorting)

- Space complexity: O(u) where u is the number of users

# Code
```javascript []
/**
 * Counts the number of mentions for each user based on events.
 *
 * @intuition
 * We need to track online status of users and process different types of events:
 * - Online events: Update a user's online status
 * - All mentions: Increment mention count for all users
 * - Here mentions: Increment mention count for all offline users
 * - Direct mentions: Increment mention count for specifically mentioned users
 *
 * @approach
 * 1. Sort events by timestamp, with tie-breaking by event type
 * 2. Process each event according to its type:
 *    - Online events: Mark user as online for 60 seconds
 *    - All mentions: Use a lazy counter to avoid processing all users immediately
 *    - Here mentions: Increment count for all offline users
 *    - Direct mentions: Extract user IDs from mentions and increment their counts
 * 3. Apply lazy counter to all users at the end
 *
 * @complexity
 * Time: O(n log n) where n is the number of events (dominated by sorting)
 * Space: O(u) where u is the number of users
 *
 * @param {number} numberOfUsers - The total number of users
 * @param {string[][]} events - Array of events, each containing [type, timestamp, data]
 * @return {number[]} - Array of mention counts for each user
 */
const countMentions = (numberOfUsers, events) => {
  // Sort events by timestamp with tie-breaking by event type
  events.sort((a, b) => {
    const timeA = +a[1];
    const timeB = +b[1];

    if (timeA === timeB) {
      return a[0].charAt(2) < b[0].charAt(2) ? -1 : 1;
    }
    return timeA - timeB;
  });

  const mentionCounts = Array(numberOfUsers).fill(0);
  const onlineTimes = Array(numberOfUsers).fill(0);
  let lazyAllMentions = 0;

  for (const [eventType, timestamp, data] of events) {
    const currentTime = +timestamp;

    if (eventType.charAt(0) === 'O') {
      // Online event: Mark user as online for 60 seconds
      const userId = +data;
      onlineTimes[userId] = currentTime + 60;
    } else if (data.charAt(0) === 'A') {
      // All mention: Increment lazy counter
      lazyAllMentions++;
    } else if (data.charAt(0) === 'H') {
      // Here mention: Increment count for offline users
      for (let i = 0; i < numberOfUsers; i++) {
        if (onlineTimes[i] <= currentTime) {
          mentionCounts[i]++;
        }
      }
    } else {
      // Direct mentions: Extract user IDs and increment their counts
      const mentions = data.split(' ');
      for (const mention of mentions) {
        const userId = +mention.slice(2);
        mentionCounts[userId]++;
      }
    }
  }

  // Apply lazy counter to all users
  if (lazyAllMentions > 0) {
    for (let i = 0; i < numberOfUsers; i++) {
      mentionCounts[i] += lazyAllMentions;
    }
  }

  return mentionCounts;
};

```
['JavaScript']