Tracking mentions in messages is a common task in communication-based applications. This blog post breaks down a complex problem, "Count Mentions Per User," and walks through how to solve it efficiently with a clear understanding of all rules and constraints.
Problem Statement
You are given:
An integer
numberOfUsersrepresenting the total number of users.An array
eventswhere each element is of sizen x 3and describes either a "MESSAGE" or an "OFFLINE" event.
Each event can be one of the following types:
MESSAGE Event:
["MESSAGE", "timestamp", "mentions_string"]Indicates that users are mentioned in a message at a specific timestamp.
The
mentions_stringcan contain:id<number>: Mentions a specific user (e.g.,id0,id1).ALL: Mentions all users (online or offline).HERE: Mentions only users who are online at the time.
OFFLINE Event:
["OFFLINE", "timestamp", "id<number>"]Indicates that a user has gone offline for 60 time units. They automatically come back online at
timestamp + 60.
Your task is to calculate the total mentions for each user across all events. The result should be an array mentions where mentions[i] represents the number of times user i is mentioned.
Constraints
All users are initially online.
Offline/online status changes are processed before any
MESSAGEevent at the same timestamp.A user can be mentioned multiple times in a single message, and all mentions are counted.
Constraints:
1 <= numberOfUsers <= 1001 <= events.length <= 1001 <= int(events[i][1]) <= 10^5mentions_stringmay contain up to 100 tokens.
Example 1:
Input: numberOfUsers = 2, events = [
["MESSAGE", "10", "id1 id0"],
["OFFLINE", "11", "0"],
["MESSAGE", "71", "HERE"]
]
Output: [2, 2]Explanation:
At timestamp 10,
id1andid0are mentioned. Mentions =[1, 1].At timestamp 11,
id0goes offline.At timestamp 71,
id0comes back online, and only online users (id0andid1) are mentioned byHERE. Mentions =[2, 2].
Example 2:
Input: numberOfUsers = 3, events = [
["MESSAGE", "2", "HERE"],
["OFFLINE", "2", "1"],
["OFFLINE", "1", "0"],
["MESSAGE", "61", "HERE"]
]
Output: [1, 0, 2]Explanation:
At timestamp 1,
id0goes offline.At timestamp 2,
id1goes offline, andHEREmentions only online users (id2). Mentions =[0, 0, 1].At timestamp 61,
id0comes back online, andHEREmentions online users (id0andid2). Mentions =[1, 0, 2].
Approach to Solve the Problem
Key Observations:
Offline and online transitions must be applied before processing any
MESSAGEevents at the same timestamp.Use a sorted order of events to ensure proper sequencing.
Maintain an array for online status and a map for tracking when users come back online.
Steps to Solve:
Sort Events:
Sort the
eventsarray by timestamp.This ensures offline/online transitions are processed before any
MESSAGEevents with the same timestamp.
Online Status Update:
Use a
mapto track the timestamp when each offline user comes back online.Before processing an event, check if any user’s offline period has ended.
Process Events:
For
OFFLINEevents:Mark the user as offline and add their return time to the
map.
For
MESSAGEevents:Handle mentions based on the type of
mentions_string(“ALL”, “HERE”, orid<number>).
Code Implementation
Complexity Analysis
Time Complexity:
Sorting the events: , where is the number of events.
Processing each event: , where is the average number of tokens in a
MESSAGE.Total: .
Space Complexity:
, where is the number of users (for
onlineandofflineEndTime).