[LeetCode] High-Access Employees

2933. High-Access Employees

You are given a 2D 0-indexed array of strings, access_times, with size n. For each i where 0 <= i <= n - 1, access_times[i][0] represents the name of an employee, and access_times[i][1] represents the access time of that employee. All entries in access_times are within the same day.

The access time is represented as four digits using a 24-hour time format, for example, "0800" or "2250".

An employee is said to be high-access if he has accessed the system three or more times within a one-hour period.

Times with exactly one hour of difference are not considered part of the same one-hour period. For example, "0815" and "0915" are not part of the same one-hour period.

Access times at the start and end of the day are not counted within the same one-hour period. For example, "0005" and "2350" are not part of the same one-hour period.

Return a list that contains the names of high-access employees with any order you want.

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
class Solution {
bool ok(vector<string>& A) {
vector<int> time;
for(auto& s : A) {
int t = (s[0] - '0') * 10 + s[1] - '0';
t *= 60;
t += (s[2] - '0') * 10 + s[3] - '0';
time.push_back(t);
}
sort(begin(time), end(time));
for(int i = 0, j = 0; j < time.size(); i++) {
while(j < time.size() and time[i] + 60 > time[j]) j += 1;
if(j - i >= 3) return true;
}
return false;
}
public:
vector<string> findHighAccessEmployees(vector<vector<string>>& access_times) {
vector<string> res;
unordered_map<string, vector<string>> mp;
for(auto& a : access_times) {
mp[a[0]].push_back(a[1]);
}
for(auto& [k,v] : mp) {
if(ok(v)) res.push_back(k);
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/11/12/PS/LeetCode/high-access-employees/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.