This event indicates that a set of users was mentioned in a message at timestampi.
The mentions_stringi string can contain one of the following tokens:
id<number>: where <number> is an integer in range [0,numberOfUsers - 1]. There can be multiple ids separated by a single whitespace and may contain duplicates. This can mention even the offline users.
ALL: mentions all users.
HERE: mentions all online users.
Offline Event:["OFFLINE", "timestampi", "idi"]
This event indicates that the user idi had become offline at timestampi for 60 time units. The user will automatically be online again at time timestampi + 60.
Return an array mentions where mentions[i] represents the number of mentions the user with id i has across all MESSAGE events.
All users are initially online, and if a user goes offline or comes back online, their status change is processed before handling any message event that occurs at the same timestamp.
Note that a user can be mentioned multiple times in a single message event, and each mention should be counted separately.
Input: numberOfUsers =2, events =[["MESSAGE","10","id1
id0"],["OFFLINE","11","0"],["MESSAGE","71","HERE"]]Output: [2,2]Explanation:
Initially, all users are online.At timestamp 10,`id1` and `id0` are mentioned.`mentions = [1,1]`At timestamp 11,`id0` goes **offline.**At timestamp 71,`id0` comes back **online** and `"HERE"`is mentioned.`mentions = [2,2]`
Input: numberOfUsers =2, events =[["MESSAGE","10","id1
id0"],["OFFLINE","11","0"],["MESSAGE","12","ALL"]]Output: [2,2]Explanation:
Initially, all users are online.At timestamp 10,`id1` and `id0` are mentioned.`mentions = [1,1]`At timestamp 11,`id0` goes **offline.**At timestamp 12,`"ALL"`is mentioned. This includes offline users, so both
`id0` and `id1` are mentioned.`mentions = [2,2]`
Input: numberOfUsers =2, events =[["OFFLINE","10","0"],["MESSAGE","12","HERE"]]Output: [0,1]Explanation:
Initially, all users are online.At timestamp 10,`id0` goes **offline.**At timestamp 12,`"HERE"`is mentioned. Because `id0`is still offline, they
will not be mentioned.`mentions = [0,1]`
Simulate the events in order, keeping track of each user’s online status and when they will come back online. For each message, count mentions according to the rules for id<number>, ALL, and HERE.
classSolution {
funcountMentionsPerUser(numberOfUsers: Int, events: Array<Array<String>>): IntArray {
dataclassEvent(val t: Int, val typ: Int, val id: Int, val s: String): Comparable<Event> {
overridefuncompareTo(other: Event): Int = if (t != other.t) t - other.t else typ - other.typ
}
val evs = events.map {
val t = it[1].toInt()
if (it[0] =="OFFLINE") Event(t, 0, it[2].toInt(), "") else Event(t, 1, -1, it[2])
}.sorted()
val mentions = IntArray(numberOfUsers)
val online = BooleanArray(numberOfUsers) { true }
val back = IntArray(numberOfUsers) { -1 }
for (ev in evs) {
for (u in0 until numberOfUsers) if (!online[u] && back[u] == ev.t) online[u] = trueif (ev.typ ==0) {
online[ev.id] = false back[ev.id] = ev.t + 60 } else {
if (ev.s.contains("ALL")) {
for (u in0 until numberOfUsers) mentions[u]++ } elseif (ev.s.contains("HERE")) {
for (u in0 until numberOfUsers) if (online[u]) mentions[u]++ } else {
for (tok in ev.s.split(" ")) {
if (tok.startsWith("id")) {
val u = tok.substring(2).toInt()
mentions[u]++ }
}
}
}
}
return mentions
}
}
classSolution:
defcountMentionsPerUser(self, numberOfUsers: int, events: list[list[str]]) -> list[int]:
evs = []
for e in events:
t = int(e[1])
if e[0] =="OFFLINE":
evs.append((t, 0, int(e[2]), ""))
else:
evs.append((t, 1, -1, e[2]))
evs.sort()
mentions = [0] * numberOfUsers
online = [True] * numberOfUsers
back = [-1] * numberOfUsers
for t, typ, idx, s in evs:
for u in range(numberOfUsers):
ifnot online[u] and back[u] == t:
online[u] =Trueif typ ==0:
online[idx] =False back[idx] = t +60else:
if"ALL"in s:
for u in range(numberOfUsers):
mentions[u] +=1elif"HERE"in s:
for u in range(numberOfUsers):
if online[u]:
mentions[u] +=1else:
for tok in s.split():
if tok.startswith("id"):
u = int(tok[2:])
mentions[u] +=1return mentions
impl Solution {
pubfncount_mentions_per_user(number_of_users: i32, events: Vec<Vec<String>>) -> Vec<i32> {
let n = number_of_users asusize;
letmut evs =vec![];
for e in events.iter() {
let t = e[1].parse::<i32>().unwrap();
if e[0] =="OFFLINE" {
evs.push((t, 0, e[2].parse::<usize>().unwrap(), String::new()));
} else {
evs.push((t, 1, usize::MAX, e[2].clone()));
}
}
evs.sort();
letmut mentions =vec![0; n];
letmut online =vec![true; n];
letmut back =vec![-1; n];
for (t, typ, idx, s) in evs {
for u in0..n {
if!online[u] && back[u] == t {
online[u] =true;
}
}
if typ ==0 {
online[idx] =false;
back[idx] = t +60;
} else {
if s.contains("ALL") {
for u in0..n { mentions[u] +=1; }
} elseif s.contains("HERE") {
for u in0..n { if online[u] { mentions[u] +=1; } }
} else {
for tok in s.split_whitespace() {
if tok.starts_with("id") {
let u = tok[2..].parse::<usize>().unwrap();
mentions[u] +=1;
}
}
}
}
}
mentions.into_iter().map(|x| x asi32).collect()
}
}