返回介绍

solution / 2900-2999 / 2933.High-Access Employees / README_EN

发布于 2024-06-17 01:02:58 字数 8377 浏览 0 评论 0 收藏 0

2933. High-Access Employees

中文文档

Description

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._

 

Example 1:

Input: access_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]]
Output: ["a"]
Explanation: "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21.
But "b" does not have more than two access times at all.
So the answer is ["a"].

Example 2:

Input: access_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]]
Output: ["c","d"]
Explanation: "c" has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29.
"d" has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08.
However, "e" has just one access time, so it can not be in the answer and the final answer is ["c","d"].

Example 3:

Input: access_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]]
Output: ["ab","cd"]
Explanation: "ab" has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24.
"cd" has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55.
So the answer is ["ab","cd"].

 

Constraints:

  • 1 <= access_times.length <= 100
  • access_times[i].length == 2
  • 1 <= access_times[i][0].length <= 10
  • access_times[i][0] consists only of English small letters.
  • access_times[i][1].length == 4
  • access_times[i][1] is in 24-hour time format.
  • access_times[i][1] consists only of '0' to '9'.

Solutions

Solution 1: Hash Table + Sorting

We use a hash table $d$ to store all access times of each employee, where the key is the employee's name, and the value is an integer array, representing all access times of the employee, which are the number of minutes from the start of the day at 00:00.

For each employee, we sort all their access times in ascending order. Then we traverse all access times of the employee. If there are three consecutive access times $t_1, t_2, t_3$ that satisfy $t_3 - t_1 < 60$, then the employee is a high-frequency visitor, and we add their name to the answer array.

Finally, we return the answer array.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of access records.

class Solution:
  def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]:
    d = defaultdict(list)
    for name, t in access_times:
      d[name].append(int(t[:2]) * 60 + int(t[2:]))
    ans = []
    for name, ts in d.items():
      ts.sort()
      if any(ts[i] - ts[i - 2] < 60 for i in range(2, len(ts))):
        ans.append(name)
    return ans
class Solution {
  public List<String> findHighAccessEmployees(List<List<String>> access_times) {
    Map<String, List<Integer>> d = new HashMap<>();
    for (var e : access_times) {
      String name = e.get(0), s = e.get(1);
      int t = Integer.valueOf(s.substring(0, 2)) * 60 + Integer.valueOf(s.substring(2));
      d.computeIfAbsent(name, k -> new ArrayList<>()).add(t);
    }
    List<String> ans = new ArrayList<>();
    for (var e : d.entrySet()) {
      String name = e.getKey();
      var ts = e.getValue();
      Collections.sort(ts);
      for (int i = 2; i < ts.size(); ++i) {
        if (ts.get(i) - ts.get(i - 2) < 60) {
          ans.add(name);
          break;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<string> findHighAccessEmployees(vector<vector<string>>& access_times) {
    unordered_map<string, vector<int>> d;
    for (auto& e : access_times) {
      auto name = e[0];
      auto s = e[1];
      int t = stoi(s.substr(0, 2)) * 60 + stoi(s.substr(2, 2));
      d[name].emplace_back(t);
    }
    vector<string> ans;
    for (auto& [name, ts] : d) {
      sort(ts.begin(), ts.end());
      for (int i = 2; i < ts.size(); ++i) {
        if (ts[i] - ts[i - 2] < 60) {
          ans.emplace_back(name);
          break;
        }
      }
    }
    return ans;
  }
};
func findHighAccessEmployees(access_times [][]string) (ans []string) {
  d := map[string][]int{}
  for _, e := range access_times {
    name, s := e[0], e[1]
    h, _ := strconv.Atoi(s[:2])
    m, _ := strconv.Atoi(s[2:])
    t := h*60 + m
    d[name] = append(d[name], t)
  }
  for name, ts := range d {
    sort.Ints(ts)
    for i := 2; i < len(ts); i++ {
      if ts[i]-ts[i-2] < 60 {
        ans = append(ans, name)
        break
      }
    }
  }
  return
}
function findHighAccessEmployees(access_times: string[][]): string[] {
  const d: Map<string, number[]> = new Map();
  for (const [name, s] of access_times) {
    const h = parseInt(s.slice(0, 2), 10);
    const m = parseInt(s.slice(2), 10);
    const t = h * 60 + m;
    if (!d.has(name)) {
      d.set(name, []);
    }
    d.get(name)!.push(t);
  }
  const ans: string[] = [];
  for (const [name, ts] of d) {
    ts.sort((a, b) => a - b);
    for (let i = 2; i < ts.length; ++i) {
      if (ts[i] - ts[i - 2] < 60) {
        ans.push(name);
        break;
      }
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文