首页 > 其他分享 >[LeetCode] 1604. Alert Using Same Key-Card Three or More Times in a One Hour Period

[LeetCode] 1604. Alert Using Same Key-Card Three or More Times in a One Hour Period

时间:2023-02-07 02:44:26浏览次数:63  
标签:10 00 name Hour hour 1604 Same keyName keyTime

LeetCode company workers use key-cards to unlock office doors. Each time a worker uses their key-card, the security system saves the worker's name and the time when it was used. The system emits an alert if any worker uses the key-card three or more times in a one-hour period.

You are given a list of strings keyName and keyTime where [keyName[i], keyTime[i]] corresponds to a person's name and the time when their key-card was used in a single day.

Access times are given in the 24-hour time format "HH:MM", such as "23:51" and "09:49".

Return a list of unique worker names who received an alert for frequent keycard use. Sort the names in ascending order alphabetically.

Notice that "10:00" - "11:00" is considered to be within a one-hour period, while "22:51" - "23:52" is not considered to be within a one-hour period.

Example 1:

Input: keyName = ["daniel","daniel","daniel","luis","luis","luis","luis"], keyTime = ["10:00","10:40","11:00","09:00","11:00","13:00","15:00"]
Output: ["daniel"]
Explanation: "daniel" used the keycard 3 times in a one-hour period ("10:00","10:40", "11:00").

Example 2:

Input: keyName = ["alice","alice","alice","bob","bob","bob","bob"], keyTime = ["12:01","12:00","18:00","21:00","21:20","21:30","23:00"]
Output: ["bob"]
Explanation: "bob" used the keycard 3 times in a one-hour period ("21:00","21:20", "21:30").

Constraints:

  • 1 <= keyName.length, keyTime.length <= 105
  • keyName.length == keyTime.length
  • keyTime[i] is in the format "HH:MM".
  • [keyName[i], keyTime[i]] is unique.
  • 1 <= keyName[i].length <= 10
  • keyName[i] contains only lowercase English letters.

警告一小时内使用相同员工卡大于等于三次的人。

力扣公司的员工都使用员工卡来开办公室的门。每当一个员工使用一次他的员工卡,安保系统会记录下员工的名字和使用时间。如果一个员工在一小时时间内使用员工卡的次数大于等于三次,这个系统会自动发布一个 警告 。

给你字符串数组 keyName 和 keyTime ,其中 [keyName[i], keyTime[i]] 对应一个人的名字和他在 某一天 内使用员工卡的时间。

使用时间的格式是 24小时制 ,形如 "HH:MM" ,比方说 "23:51" 和 "09:49" 。

请你返回去重后的收到系统警告的员工名字,将它们按 字典序升序 排序后返回。

请注意 "10:00" - "11:00" 视为一个小时时间范围内,而 "23:51" - "00:10" 不被视为一小时内,因为系统记录的是某一天内的使用情况。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是用 hashmap 记录每个不同的人所有的打卡时间,然后看看谁的打卡时间会触发警报。这里处理打卡时间需要有一点技巧,因为时间是 24 小时的格式,而 "23:51" - "00:10" 不被视为是同一天的时间。对于时间戳,我们可以把他转换成 hour * 60 + minutes,这样,越晚的时间,这个值就越大,而且不会造成 "23:51" - "00:10" 被视为是同一天的困扰。其他部分都不难,最后注意需要对 output 排序。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public List<String> alertNames(String[] keyName, String[] keyTime) {
 3         int n = keyName.length;
 4         Map<String, List<Integer>> map = new HashMap<>();
 5         List<String> res = new ArrayList<>();
 6         for (int i = 0; i < n; i++) {
 7             String name = keyName[i];
 8             if (!map.containsKey(name)) {
 9                 map.put(name, new ArrayList<>());
10             }
11             map.get(name).add(getTime(keyTime[i]));
12         }
13 
14         for (String name : map.keySet()) {
15             List<Integer> list = map.get(name);
16             Collections.sort(list);
17             for (int i = 2; i < list.size(); i++) {
18                 if (list.get(i) - list.get(i - 2) <= 60) {
19                     res.add(name);
20                     break;
21                 }
22             }
23         }
24         Collections.sort(res);
25         return res;
26     }
27 
28     private int getTime(String s) {
29         String[] ss = s.split(":");
30         return 60 * Integer.parseInt(ss[0]) + Integer.parseInt(ss[1]);
31     }
32 }

 

LeetCode 题目总结

标签:10,00,name,Hour,hour,1604,Same,keyName,keyTime
From: https://www.cnblogs.com/cnoodle/p/17097137.html

相关文章