-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3433.cpp
More file actions
55 lines (55 loc) · 1.83 KB
/
3433.cpp
File metadata and controls
55 lines (55 loc) · 1.83 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
class Solution {
public:
static const bool compare(vector<string>& a, vector<string>& b) {
int aa = stoi(a[1]);
int bb = stoi(b[1]);
if (aa == bb) {
return b[0] == "MESSAGE";
}
return aa < bb;
}
vector<int> countMentions(int numberOfUsers, vector<vector<string>>& events) {
sort(events.begin(), events.end(), compare);
int now = 0;
queue<pair<int, int>> q;
unordered_set<int> st;
unordered_map<int, int> offlineCnt;
for (int i = 0; i < numberOfUsers; ++i) st.insert(i);
vector<int> res(numberOfUsers, 0);
for (auto& event : events) {
string command = event[0];
int timestamp = stoi(event[1]);
string ids = event[2];
if (command == "OFFLINE") {
int id = stoi(ids);
q.push({timestamp + 60, id});
st.erase(id);
offlineCnt[id]++;
}
else {
now = timestamp;
while (q.size() > 0 && q.front().first <= now) {
pair<int, int> p = q.front();
q.pop();
offlineCnt[p.second]--;
if (offlineCnt[p.second] == 0) st.insert(p.second);
}
if (ids == "ALL") {
for (int i = 0; i < numberOfUsers; ++i) res[i]++;
}
else if (ids == "HERE") {
for (int i : st) res[i]++;
}
else {
stringstream ss(ids);
string id;
while (getline(ss, id, ' ')) {
int idInt = stoi(id.substr(2));
res[idInt]++;
}
}
}
}
return res;
}
};