You are given an integer numberOfUsers representing the total number of users and an array events of size n x 3.
Each events[i] can be either of the following two types:
["MESSAGE", "timestampi", "mentions_stringi"]
timestampi.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", "timestampi", "idi"]
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.
Example 1:
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]
Example 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]
Example 3:
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]
Constraints:
1 <= numberOfUsers <= 1001 <= events.length <= 100events[i].length == 3events[i][0] will be one of MESSAGE or OFFLINE.1 <= int(events[i][1]) <= 105id<number> mentions in any "MESSAGE" event is between 1 and 100.0 <= <number> <= numberOfUsers - 1OFFLINE event is online at the time the event occurs.class Solution {
public int[] countMentions(int numberOfUsers, List<List<String>> events) {
int n = numberOfUsers, globalMention = 0;
int[] mentions = new int[n];
int[] active = new int[n];
Queue<List<String>> q = new PriorityQueue<>((a, b) -> a.get(1).equals(b.get(1))
? b.get(0).compareTo(a.get(0))
: Integer.parseInt(a.get(1)) - Integer.parseInt(b.get(1))); // tsp asc
q.addAll(events);
// System.out.println(q);
while (!q.isEmpty()) {
List<String> top = q.poll();
if (top.get(0).equals("OFFLINE")) {
active[Integer.parseInt(top.get(2))] = Integer.parseInt(top.get(1)) + 60;
} else {
int tsp = Integer.parseInt(top.get(1));
String[] ids = top.get(2).split(" ");
for (String id : ids) {
if (id.equals("HERE")) {
for (int i = 0; i < n; i++)
if (active[i] <= tsp)
mentions[i]++;
} else if (id.equals("ALL"))
globalMention++;
else
mentions[Integer.parseInt(id.substring(2))]++;
}
}
}
for (int i = 0; i < n; i++)
mentions[i] += globalMention;
return mentions;
}
}class Solution {
public int[] countMentions(int numberOfUsers, List<List<String>> events) {
int n = numberOfUsers, globalMention = 0;
int[] mentions = new int[n];
int[] active = new int[n];
Queue<List<String>> q = new PriorityQueue<>((a, b) -> a.get(1).equals(b.get(1))
? b.get(0).compareTo(a.get(0))
: Integer.parseInt(a.get(1)) - Integer.parseInt(b.get(1))); // tsp asc
q.addAll(events);
// System.out.println(q);
while (!q.isEmpty()) {
List<String> top = q.poll();
if (top.get(0).equals("OFFLINE")) {
active[Integer.parseInt(top.get(2))] = Integer.parseInt(top.get(1)) + 60;
} else {
int tsp = Integer.parseInt(top.get(1));
String[] ids = top.get(2).split(" ");
for (String id : ids) {
if (id.equals("HERE")) {
for (int i = 0; i < n; i++)
if (active[i] <= tsp)
mentions[i]++;
} else if (id.equals("ALL"))
globalMention++;
else
mentions[Integer.parseInt(id.substring(2))]++;
}
}
}
for (int i = 0; i < n; i++)
mentions[i] += globalMention;
return mentions;
}
}