Skip to content

Commit 4c37887

Browse files
authored
Create 3433. Count Mentions Per User (#956)
2 parents d47c3e4 + d097ebf commit 4c37887

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

3433. Count Mentions Per User

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
vector<int> countMentions(int numberOfUsers, vector<vector<string>>& events) {
7+
map<int, vector<vector<string>>> byTime;
8+
for (auto &ev : events) {
9+
int t = stoi(ev[1]);
10+
byTime[t].push_back(ev);
11+
}
12+
13+
vector<int> mentions(numberOfUsers, 0);
14+
vector<bool> isOnline(numberOfUsers, true);
15+
vector<int> offlineUntil(numberOfUsers, 0);
16+
17+
for (auto &entry : byTime) {
18+
int t = entry.first;
19+
auto &evs = entry.second;
20+
21+
for (int i = 0; i < numberOfUsers; ++i) {
22+
if (!isOnline[i] && offlineUntil[i] <= t) {
23+
isOnline[i] = true;
24+
offlineUntil[i] = 0;
25+
}
26+
}
27+
28+
for (auto &ev : evs) {
29+
if (ev[0] == "OFFLINE") {
30+
int id = stoi(ev[2]);
31+
isOnline[id] = false;
32+
offlineUntil[id] = t + 60;
33+
}
34+
}
35+
36+
for (auto &ev : evs) {
37+
if (ev[0] != "MESSAGE") continue;
38+
string mentionsStr = ev[2];
39+
string token;
40+
stringstream ss(mentionsStr);
41+
while (ss >> token) {
42+
if (token == "ALL") {
43+
for (int i = 0; i < numberOfUsers; ++i) mentions[i]++;
44+
} else if (token == "HERE") {
45+
for (int i = 0; i < numberOfUsers; ++i)
46+
if (isOnline[i]) mentions[i]++;
47+
} else if (token.rfind("id", 0) == 0) {
48+
int id = stoi(token.substr(2));
49+
if (0 <= id && id < numberOfUsers) mentions[id]++;
50+
}
51+
}
52+
}
53+
}
54+
55+
return mentions;
56+
}
57+
};

0 commit comments

Comments
 (0)