LeetCode 题解工作台

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

力扣公司的员工都使用员工卡来开办公室的门。每当一个员工使用一次他的员工卡,安保系统会记录下员工的名字和使用时间。如果一个员工在一小时时间内使用员工卡的次数大于等于三次,这个系统会自动发布一个 警告 。 给你字符串数组 keyName 和 keyTime ,其中 [keyName[i], keyTim…

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们先用哈希表 记录每个员工的所有打卡时间。 然后遍历哈希表,对于每个员工,我们先判断员工的打卡次数是否大于等于 3,如果不是,则跳过该员工。否则,我们将该员工的所有打卡时间按照时间先后排序,然后遍历排序后的打卡时间,判断下标距离为 的两个时间是否在同一小时内,如果是,则将该员工加入答案数组。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

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

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

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

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

请注意 "10:00" - "11:00" 视为一个小时时间范围内,而 "22:51" - "23:52" 不被视为一小时时间范围内。

 

示例 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")。

 

提示:

  • 1 <= keyName.length, keyTime.length <= 105
  • keyName.length == keyTime.length
  • keyTime 格式为 "HH:MM" 
  • 保证 [keyName[i], keyTime[i]] 形成的二元对 互不相同 
  • 1 <= keyName[i].length <= 10
  • keyName[i] 只包含小写英文字母。
lightbulb

解题思路

方法一:哈希表 + 排序

我们先用哈希表 dd 记录每个员工的所有打卡时间。

然后遍历哈希表,对于每个员工,我们先判断员工的打卡次数是否大于等于 3,如果不是,则跳过该员工。否则,我们将该员工的所有打卡时间按照时间先后排序,然后遍历排序后的打卡时间,判断下标距离为 22 的两个时间是否在同一小时内,如果是,则将该员工加入答案数组。

最后,将答案数组按照字典序排序,即可得到答案。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(n)O(n)。其中 nn 是打卡记录的数量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
    def alertNames(self, keyName: List[str], keyTime: List[str]) -> List[str]:
        d = defaultdict(list)
        for name, t in zip(keyName, keyTime):
            t = int(t[:2]) * 60 + int(t[3:])
            d[name].append(t)
        ans = []
        for name, ts in d.items():
            if (n := len(ts)) > 2:
                ts.sort()
                for i in range(n - 2):
                    if ts[i + 2] - ts[i] <= 60:
                        ans.append(name)
                        break
        ans.sort()
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    You notice quickly that the alert condition is per name, so the first move is grouping by employee before doing any time logic.

  • question_mark

    You convert "HH:MM" into minutes early, which avoids brittle string comparisons and makes the 60-minute rule exact.

  • question_mark

    You justify why checking only sorted consecutive triples is enough, instead of comparing every combination of three accesses.

warning

常见陷阱

外企场景
  • error

    Treating one hour as needing the same clock hour, which incorrectly rejects windows like 10:40, 10:59, and 11:00.

  • error

    Sorting all records globally by time and trying to track alerts across names, which mixes unrelated employees and complicates the logic.

  • error

    Checking adjacent pairs instead of triples, which misses the actual trigger condition of three or more uses within one hour.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Return the actual triggering time window for each employee instead of only the employee names.

  • arrow_right_alt

    Raise an alert after k accesses within t minutes, which generalizes the same grouped sorting pattern to a wider sliding window.

  • arrow_right_alt

    Handle multi-day logs, where converting times requires attaching a date so midnight crossings do not break minute comparisons.

help

常见问题

外企场景

警告一小时内使用相同员工卡大于等于三次的人题解:数组·哈希·扫描 | LeetCode #1604 中等