class Solution {
public:
vector<string> alertNames(vector<string>& keyName, vector<string>& keyTime) {
}
};
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 <= 105
keyName.length == keyTime.length
keyTime
格式为 "HH:MM" 。[keyName[i], keyTime[i]]
形成的二元对 互不相同 。1 <= keyName[i].length <= 10
keyName[i]
只包含小写英文字母。原站题解
java 解法, 执行用时: 56 ms, 内存消耗: 59.8 MB, 提交时间: 2023-02-07 09:44:10
class Solution { public List<String> alertNames(String[] keyName, String[] keyTime) { Map<String, List<Integer>> d = new HashMap<>(); for (int i = 0; i < keyName.length; ++i) { String name = keyName[i]; String time = keyTime[i]; int t = Integer.parseInt(time.substring(0, 2)) * 60 + Integer.parseInt(time.substring(3)); d.computeIfAbsent(name, k -> new ArrayList<>()).add(t); } List<String> ans = new ArrayList<>(); for (var e : d.entrySet()) { var ts = e.getValue(); int n = ts.size(); if (n > 2) { Collections.sort(ts); for (int i = 0; i < n - 2; ++i) { if (ts.get(i + 2) - ts.get(i) <= 60) { ans.add(e.getKey()); break; } } } } Collections.sort(ans); return ans; } }
golang 解法, 执行用时: 260 ms, 内存消耗: 18.6 MB, 提交时间: 2023-02-07 09:43:15
func alertNames(keyName []string, keyTime []string) (ans []string) { d := map[string][]int{} for i, name := range keyName { var a, b int fmt.Sscanf(keyTime[i], "%d:%d", &a, &b) t := a*60 + b d[name] = append(d[name], t) } for name, ts := range d { n := len(ts) if n > 2 { sort.Ints(ts) for i := 0; i < n-2; i++ { if ts[i+2]-ts[i] <= 60 { ans = append(ans, name) break } } } } sort.Strings(ans) return }
python3 解法, 执行用时: 500 ms, 内存消耗: 44.5 MB, 提交时间: 2020-11-06 12:57:53
class Solution: def change_to_int(self,a: str) -> int: return int(a[0]) * 10 * 60 + int(a[1]) * 60 + int(a[3]) * 10 + int(a[4]) def alertNames(self, keyName: List[str], keyTime: List[str]) -> List[str]: d = [[keyName[i],self.change_to_int(keyTime[i])] for i in range(len(keyName))] ans = [] d.sort(key = lambda a: (a[0], a[1])) for i in range(2, len(keyName)): if len(ans) and ans[-1] == d[i][0]: continue if d[i][0] == d[i-2][0] and d[i][1] - d[i-2][1] <= 60: ans.append(d[i][0]) return ans
python3 解法, 执行用时: 320 ms, 内存消耗: 35.4 MB, 提交时间: 2020-11-06 12:49:18
class Solution: def helper(self, hhmm: str) -> int: h, m = hhmm.split(':') return (int(h) if h[0] != '0' else int(h[1])) * 60 + (int(m) if m[0] != '0' else int(m[1])) def alertNames(self, keyName: List[str], keyTime: List[str]) -> List[str]: keyTime = [self.helper(i) for i in keyTime] map_ = dict() res = [] for i, name in enumerate(keyName): map_[name] = map_[name] + [keyTime[i]] if name in map_.keys() else [keyTime[i]] for name, t in map_.items(): if len(t) < 3: continue t.sort() for j in range(0, len(t)-2): if t[j] < t[j+1] < t[j+2] and t[j+2] - t[j] < 61: res.append(name) break res.sort() return res