【每日一题Day111】LC1604警告一小时内使用相同员工卡大于等于三次的人 | 哈希表+排序

简介: 使用哈希表统计每位员工使用工卡的所有时间节点,将时间转化为分钟的形式,方便后续统计一小时内使用次数大于等于3的员工

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


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.


2023/2/7


  • 思路:


。使用哈希表统计每位员工使用工卡的所有时间节点,将时间转化为分钟的形式,方便后续统计一小时内使用次数大于等于3的员工


。然后将每位员工的时间节点从小到大排序,如果在60分钟的间隔内,有大于等于3个时间节点,那么需要对该员工发出警告


  • 双指针滑动窗口统计
  • 枚举每一个时间节点i ii,判断其与第i−1个时间节点的差值是否小于等于60


。返回结果需要按照字典顺序排序


  • 实现


class Solution {
    public List<String> alertNames(String[] keyName, String[] keyTime) {
        Map<String,List<Integer>> map = new HashMap<>();;
        List<String> res = new LinkedList<>();
        int n = keyName.length;        
        for (int i = 0; i < n; i++){
            String name = keyName[i];
            String[] times = keyTime[i].split(":");
            int hour = Integer.parseInt(times[0]);
            int m = Integer.parseInt(times[1]);
            int t = hour * 60 + m;
            List<Integer> list = map.getOrDefault(name, new ArrayList<>());
            list.add(t);
            map.put(name, list);
        }
        for (var node : map.entrySet()){
            String name = node.getKey();
            List<Integer> times = node.getValue();
            int size = times.size();            
            Collections.sort(times);
            // 滑动窗口
            // if (size < 3) continue;
            // int l = 0, r = 0;
            // while (l < size && r < size){
            //     while (r < size && times.get(r) - times.get(l) <= 60 ){
            //         r++;
            //     } 
            //     if (r - l >= 3){
            //         res.add(name);
            //         break;
            //     }
            //     while (l < size && r < size && times.get(r) - times.get(l) > 60){
            //         l++;
            //     } 
            // }
            // 枚举
            for (int i = 2; i < size; i++){
                if (times.get(i) - times.get(i - 2) <= 60){
                    res.add(name);
                    break;
                }
            }
        }
        Collections.sort(res);
        return res;
    }
}


。复杂度


  • 时间复杂度:O ( n l o g n )
  • 空间复杂度:O ( n )
目录
相关文章
|
4月前
【每日一题Day199】LC1010总持续时间可被 60 整除的歌曲 | 哈希表
【每日一题Day199】LC1010总持续时间可被 60 整除的歌曲 | 哈希表
21 1
|
4月前
【每日一题Day118】LC1124表现良好的最长时间段 | 前缀和+单调栈/哈希表
【每日一题Day118】LC1124表现良好的最长时间段 | 前缀和+单调栈/哈希表
30 0
|
4月前
【每日一题Day278】LC2500删除每行中的最大值 | 排序+模拟
【每日一题Day278】LC2500删除每行中的最大值 | 排序+模拟
19 0
|
4月前
【每日一题Day197】LC2432处理用时最长的那个任务的员工 | 枚举
【每日一题Day197】LC2432处理用时最长的那个任务的员工 | 枚举
27 0
|
4月前
【每日一题Day227】LC2465不同的平均值数目 | 排序 + 哈希表
【每日一题Day227】LC2465不同的平均值数目 | 排序 + 哈希表
17 0
|
4月前
【每日一题Day215】LC1090受标签影响的最大值 | 贪心+排序+哈希表
【每日一题Day215】LC1090受标签影响的最大值 | 贪心+排序+哈希表
18 0
|
9月前
|
机器学习/深度学习 Cloud Native
【刷题日记】2044. 统计按位或能得到最大值的子集数目
本次刷题日记的第 8 篇,力扣题为:2044. 统计按位或能得到最大值的子集数目 ,中等
|
算法 Java
数组中出现次数超过一半的数字(剑指offer39 力扣169)Java哈希表、排序、摩尔投票
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。
【每日一题Day93】LC1817查找用户活跃分钟数 | 哈希表
思路:使用哈希表记录每个用户的活跃时间,那么用户活跃分钟数则为活跃时间的多少,因此哈希表的key值为用户id,value值为Set集合
62 0
|
存储
【每日一题Day90】LC1814统计一个数组中好对子的数目 | 哈希表
思路:如果两个数满足好对子,那么这两个数反转后的变化量相同。因此可以使用哈希表存放反转后的变化量及其次数count,该变化量存在的所有好对子个数为count∗(count−1)/2
53 0