5516. 警告一小時內使用相同員工卡大於等於三次的人-map/優先佇列-中等
問題描述
力扣公司的員工都使用員工卡來開辦公室的門。每當一個員工使用一次他的員工卡,安保系統會記錄下員工的名字和使用時間。如果一個員工在一小時時間內使用員工卡的次數大於等於三次,這個系統會自動釋出一個 警告。
給你字串陣列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]只包含小寫英文字母。
來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period
解答
//樸實無華的map硬懟,用了下優先佇列,這樣就可以省去對每個人打卡時間的排序了 class Solution { public int minites(String time){ return Integer.parseInt(time.substring(0,2))*60+Integer.parseInt(time.substring(3,5)); } public List<String> alertNames(String[] keyName, String[] keyTime) { List<String> res = new ArrayList<>(); if(keyName.length!=keyTime.length || keyName.length==0)return res; Map<String, Queue<Integer>> map = new HashMap<>(); int count = 0; for(String name:keyName){ if(!map.containsKey(name))map.put(name, new PriorityQueue<Integer>()); map.get(name).offer(minites(keyTime[count])); count++; } for(String name:map.keySet()){ Queue<Integer> hour = new LinkedList<>(); Queue<Integer> temp = map.get(name); while(!temp.isEmpty()){ int next = temp.poll(); if(hour.isEmpty())hour.offer(next); else{ while(!hour.isEmpty() && next-hour.peek()>60){ hour.poll(); } hour.offer(next); } if(hour.size()>=3){ res.add(name); break; } } } Collections.sort(res); return res; } }