- Alert Using Same Key-Card Three or More Times in a One Hour Period
Medium
98
174
Add to List
Share
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").
Example 3:
Input: keyName = ["john","john","john"], keyTime = ["23:58","23:59","00:01"]
Output: []
Example 4:
Input: keyName = ["leslie","leslie","leslie","clare","clare","clare","clare"], keyTime = ["13:00","13:20","14:00","18:00","18:51","19:30","19:49"]
Output: ["clare","leslie"]
/*
1.map存 map<String,List<Integer>>.
第一步把名字,时间转化成数字放到map里面。
2.对map进行便历
取每个map.entrySet中的Entry,entry.getValue为时间列表。
用size为3的滑动窗口,如果list.get(i)-list.get(i-2)的值<=60则把这个人家加到警告列表。
*/
class Solution {
public List<String> alertNames(String[] keyName, String[] keyTime) {
Map<String, List<Integer>> nameAndTimes = new HashMap<>();
for (int i = 0; i < keyName.length; i++) {
Integer time = Integer.parseInt(keyTime[i].substring(0, 2)) * 60
+ Integer.parseInt(keyTime[i].substring(3));
nameAndTimes.computeIfAbsent(keyName[i], k -> new ArrayList<Integer>()).add(time);
}
List<String>warningList =new ArrayList<>();
for (Map.Entry<String, List<Integer>> nameAndTime : nameAndTimes.entrySet()) {
List<Integer> times = nameAndTime.getValue();
for(int i=2; i<times.size();i++) {
if(times.get(i)-times.get(i-2)<=60) {
warningList.add(nameAndTime.getKey());
break;
}
}
}
warningList.sort(Comparator.naturalOrder());
return warningList;
}
新的语法糖
computeIfAbsent
1.如果当前key已经有值了就会返回现有的value.
2.如果当前key没有值,用给的函数,新建一个值。
// 方法定义
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
...
}
Returns:
the current (existing or computed) value associated with the specified key, or null if the computed value is null
// java8之前。从map中根据key获取value操作可能会有下面的操作
List<Integer> value = map.get("key");
if (value == null) {
value = new ArrayList<Integer>();
map.put("key", value);
}
value.add(someInteger);
// java8之后。上面的操作可以简化为一行,若key对应的value为空,会将第二个参数的返回值存入并返回
List<Integer> value = map.computeIfAbsent("key", k -> new Object());
1604 Alert Using Same Key-Card Three or More Times in a One Hour Period