LeetCode 1604: Alert Using Same Key-Card Three or More Times in a One Hour Period Solution
Master LeetCode problem 1604 (Alert Using Same Key-Card Three or More Times in a One Hour Period), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1604. Alert Using Same Key-Card Three or More Times in a One Hour Period
Problem Explanation
Explanation:
To solve this problem, we can follow these steps:
- Create a map to store the keycard usage times for each worker.
- Sort the keycard usage times for each worker.
- Iterate over the sorted times for each worker and check if there are three consecutive times within a one-hour period.
- If such a pattern is found, add the worker to the result list.
- Sort the final result list and return it.
Time Complexity: O(n log n) where n is the total number of keycard usage times.
Space Complexity: O(n) for storing the keycard usage times.
:
Solution Code
class Solution {
public List<String> alertNames(String[] keyName, String[] keyTime) {
Map<String, List<Integer>> map = new HashMap<>();
List<String> result = new ArrayList<>();
for (int i = 0; i < keyName.length; i++) {
map.putIfAbsent(keyName[i], new ArrayList<>());
map.get(keyName[i]).add(Integer.parseInt(keyTime[i].substring(0, 2)) * 60 + Integer.parseInt(keyTime[i].substring(3)));
}
for (Map.Entry<String, List<Integer>> entry : map.entrySet()) {
List<Integer> times = entry.getValue();
Collections.sort(times);
for (int i = 2; i < times.size(); i++) {
if (times.get(i) - times.get(i - 2) <= 60) {
result.add(entry.getKey());
break;
}
}
}
Collections.sort(result);
return result;
}
}
Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1604 (Alert Using Same Key-Card Three or More Times in a One Hour Period)?
This page provides optimized solutions for LeetCode problem 1604 (Alert Using Same Key-Card Three or More Times in a One Hour Period) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1604 (Alert Using Same Key-Card Three or More Times in a One Hour Period)?
The time complexity for LeetCode 1604 (Alert Using Same Key-Card Three or More Times in a One Hour Period) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1604 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1604 in Java, C++, or Python.