forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
high-access-employees.cpp
31 lines (28 loc) · 956 Bytes
/
high-access-employees.cpp
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
// Time: O(nlogn)
// Space: O(n)
// sort, two pointers, sliding window
class Solution {
public:
vector<string> findHighAccessEmployees(vector<vector<string>>& access_times) {
static const int LIMIT_COUNT = 2;
static const int LIMIT_MINUTE = 60;
const auto& to_minute = [](const auto& s) {
return stoi(s.substr(0, 2)) * 60 + stoi(s.substr(2));
};
unordered_map<string, vector<int>> lookup;;
for (const auto& x : access_times) {
lookup[x[0]].emplace_back(to_minute(x[1]));
}
vector<string> result;
for (auto& [x, ts] : lookup) {
sort(begin(ts), end(ts));
for (int i = 0; i + LIMIT_COUNT < size(ts); ++i) {
if (!(ts[i] + LIMIT_MINUTE <= ts[i + LIMIT_COUNT])) {
result.emplace_back(x);
break;
}
}
}
return result;
}
};