【算法题】1604. 警告一小时内使用相同员工卡大于等于三次的人
插: 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。
坚持不懈,越努力越幸运,大家一起学习鸭~~~
题目:
力扣公司的员工都使用员工卡来开办公室的门。每当一个员工使用一次他的员工卡,安保系统会记录下员工的名字和使用时间。如果一个员工在一小时时间内使用员工卡的次数大于等于三次,这个系统会自动发布一个 警告 。
给你字符串数组 keyName 和 keyTime ,其中 [keyName[i], keyTime[i]] 对应一个人的名字和他在 某一天 内使用员工卡的时间。
使用时间的格式是 24小时制 ,形如 "HH:MM" ,比方说 "23:51" 和 "09:49" 。
请你返回去重后的收到系统警告的员工名字,将它们按 字典序升序 排序后返回。
请注意 "10:00" - "11:00" 视为一个小时时间范围内,而 "23:51" - "00:10" 不被视为一小时内,因为系统记录的是某一天内的使用情况。
示例 1:
输入:keyName = ["daniel","daniel","daniel","luis","luis","luis","luis"], keyTime = ["10:00","10:40","11:00","09:00","11:00","13:00","15:00"]
输出:["daniel"]
解释:"daniel" 在一小时内使用了 3 次员工卡("10:00","10:40","11:00")。
示例 2:
输入:keyName = ["alice","alice","alice","bob","bob","bob","bob"], keyTime = ["12:01","12:00","18:00","21:00","21:20","21:30","23:00"]
输出:["bob"]
解释:"bob" 在一小时内使用了 3 次员工卡("21:00","21:20","21:30")。
示例 3:
输入:keyName = ["john","john","john"], keyTime = ["23:58","23:59","00:01"]
输出:[]
示例 4:
输入:keyName = ["leslie","leslie","leslie","clare","clare","clare","clare"], keyTime = ["13:00","13:20","14:00","18:00","18:51","19:30","19:49"]
输出:["clare","leslie"]
提示:
1 <= keyName.length, keyTime.length <= 10^5
keyName.length == keyTime.length
keyTime 格式为 "HH:MM" 。
保证 [keyName[i], keyTime[i]] 形成的二元对 互不相同 。
1 <= keyName[i].length <= 10
keyName[i] 只包含小写英文字母。
java代码:
class Solution {
public List<String> alertNames(String[] keyName, String[] keyTime) {
String[][] nameTime = this.zip(keyName, keyTime);
Arrays.sort(nameTime, (a, b) -> a[0].equals(b[0]) ? a[1].compareTo(b[1]) : a[0].compareTo(b[0]));
List<String> ans = new ArrayList<>();
String who = "";
boolean add = false;
int[] times = new int[3];
for (int i = 0, cnt = 0; i < nameTime.length; i++) {
String name = nameTime[i][0];
int time = this.time(nameTime[i][1]);
if (name.equals(who)) {
if (!add) {
times[cnt % 3] = time;
if (cnt > 1 && times[cnt % 3] - times[(cnt + 2) % 3] <= 60
&& times[cnt % 3] - times[(cnt + 1) % 3] <= 60) {
ans.add(name);
add = true;
}
cnt++;
}
} else {
add = false;
who = name;
cnt = 0;
times[cnt++] = time;
}
}
return ans;
}
private int time(String time) {
return Integer.valueOf(time.substring(0, 2)) * 60 + Integer.valueOf(time.substring(3, 5));
}
public String[][] zip(String[] keyName, String[] keyTime) {
int len = keyName.length;
String[][] zip = new String[len][2];
for (int i = 0; i < zip.length; i++) {
zip[i][0] = keyName[i];
zip[i][1] = keyTime[i];
}
return zip;
}
}