-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12_Count_Mentions_Per_User.cpp
More file actions
92 lines (80 loc) · 2.67 KB
/
Copy path12_Count_Mentions_Per_User.cpp
File metadata and controls
92 lines (80 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 3433. Count Mentions Per User
class Solution
{
public:
vector<int> countMentions(int numberOfUsers, vector<vector<string>> &events)
{
// Sort by timestamp, and OFFLINE before MESSAGE when equal
sort(events.begin(), events.end(),
[](const vector<string> &a, const vector<string> &b)
{
int t1 = stoi(a[1]);
int t2 = stoi(b[1]);
if (t1 != t2)
return t1 < t2;
// Same timestamp → OFFLINE before MESSAGE
if (a[0] == "OFFLINE" && b[0] == "MESSAGE")
return true;
if (a[0] == "MESSAGE" && b[0] == "OFFLINE")
return false;
return false;
});
vector<int> mentions(numberOfUsers, 0);
vector<bool> online(numberOfUsers, true);
vector<int> backOnlineTime(numberOfUsers, -1);
for (auto &event : events)
{
string type = event[0];
int timestamp = stoi(event[1]);
// Update auto-online users first
for (int i = 0; i < numberOfUsers; i++)
{
if (!online[i] && backOnlineTime[i] != -1 && timestamp >= backOnlineTime[i])
{
online[i] = true;
backOnlineTime[i] = -1;
}
}
if (type == "OFFLINE")
{
int id = stoi(event[2]);
online[id] = false;
backOnlineTime[id] = timestamp + 60;
}
else
{ // MESSAGE
string msg = event[2];
if (msg == "ALL")
{
for (int i = 0; i < numberOfUsers; i++)
{
mentions[i]++;
}
}
else if (msg == "HERE")
{
for (int i = 0; i < numberOfUsers; i++)
{
if (online[i])
mentions[i]++;
}
}
else
{
// Parse tokens: id0 id1 id2 ...
stringstream ss(msg);
string tok;
while (ss >> tok)
{
if (tok.rfind("id", 0) == 0)
{ // starts with "id"
int uid = stoi(tok.substr(2));
mentions[uid]++; // count duplicates too
}
}
}
}
}
return mentions;
}
};