1604. Alert Using Same Key-Card Three or More Times in a One Hour Period LeetCode Solution

In this guide, you will get 1604. Alert Using Same Key-Card Three or More Times in a One Hour Period LeetCode Solution with the best time and space complexity. The solution to Alert Using Same Key-Card Three or More Times in a One Hour Period problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Alert Using Same Key-Card Three or More Times in a One Hour Period solution in C++
  4. Alert Using Same Key-Card Three or More Times in a One Hour Period solution in Java
  5. Alert Using Same Key-Card Three or More Times in a One Hour Period solution in Python
  6. Additional Resources
1604. Alert Using Same Key-Card Three or More Times in a One Hour Period LeetCode Solution image

Problem Statement of Alert Using Same Key-Card Three or More Times in a One Hour Period

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.

See also  1504. Count Submatrices With All Ones LeetCode Solution

Example 1:

Input: keyName = [“daniel”,”daniel”,”daniel”,”luis”,”luis”,”luis”,”luis”], keyTime = [“10:00″,”10:40″,”11:00″,”09:00″,”11:00″,”13:00″,”15:00”]
Output: [“daniel”]
Explanation: “daniel” used the keycard 3 times in a one-hour period (“10:00″,”10:40”, “11:00”).

Example 2:

Input: keyName = [“alice”,”alice”,”alice”,”bob”,”bob”,”bob”,”bob”], keyTime = [“12:01″,”12:00″,”18:00″,”21:00″,”21:20″,”21:30″,”23:00”]
Output: [“bob”]
Explanation: “bob” used the keycard 3 times in a one-hour period (“21:00″,”21:20”, “21:30”).

Constraints:

1 <= keyName.length, keyTime.length <= 105
keyName.length == keyTime.length
keyTime[i] is in the format "HH:MM".
[keyName[i], keyTime[i]] is unique.
1 <= keyName[i].length <= 10
keyName[i] contains only lowercase English letters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1604. Alert Using Same Key-Card Three or More Times in a One Hour Period LeetCode Solution in C++

class Solution {
 public:
  vector<string> alertNames(vector<string>& keyName, vector<string>& keyTime) {
    vector<string> ans;
    unordered_map<string, vector<int>> nameToMinutes;

    for (int i = 0; i < keyName.size(); ++i) {
      const int minutes = getMinutes(keyTime[i]);
      nameToMinutes[keyName[i]].push_back(minutes);
    }

    for (auto& [name, minutes] : nameToMinutes)
      if (hasAlert(minutes))
        ans.push_back(name);

    ranges::sort(ans);
    return ans;
  }

 private:
  // Returns true if any worker uses the key-card three or more times in an
  // one-hour period.
  bool hasAlert(vector<int>& minutes) {
    if (minutes.size() > 70)
      return true;
    ranges::sort(minutes);
    for (int i = 2; i < minutes.size(); ++i)
      if (minutes[i - 2] + 60 >= minutes[i])
        return true;
    return false;
  }

  int getMinutes(const string& time) {
    const int h = stoi(time.substr(0, 2));
    const int m = stoi(time.substr(3));
    return 60 * h + m;
  }
};
/* code provided by PROGIEZ */

1604. Alert Using Same Key-Card Three or More Times in a One Hour Period LeetCode Solution in Java

class Solution {
  public List<String> alertNames(String[] keyName, String[] keyTime) {
    List<String> ans = new ArrayList<>();
    HashMap<String, List<Integer>> nameToMinutes = new HashMap<>();

    for (int i = 0; i < keyName.length; i++) {
      final int minutes = getMinutes(keyTime[i]);
      nameToMinutes.putIfAbsent(keyName[i], new ArrayList<>());
      nameToMinutes.get(keyName[i]).add(minutes);
    }

    for (Map.Entry<String, List<Integer>> entry : nameToMinutes.entrySet()) {
      final String name = entry.getKey();
      List<Integer> minutes = entry.getValue();
      if (hasAlert(minutes))
        ans.add(name);
    }

    Collections.sort(ans);
    return ans;
  }

  private boolean hasAlert(List<Integer> minutes) {
    if (minutes.size() > 70)
      return true;
    Collections.sort(minutes);
    for (int i = 2; i < minutes.size(); i++)
      if (minutes.get(i - 2) + 60 >= minutes.get(i))
        return true;
    return false;
  }

  private int getMinutes(String time) {
    final int h = Integer.parseInt(time.substring(0, 2));
    final int m = Integer.parseInt(time.substring(3));
    return 60 * h + m;
  }
}
// code provided by PROGIEZ

1604. Alert Using Same Key-Card Three or More Times in a One Hour Period LeetCode Solution in Python

class Solution:
  def alertNames(self, keyName: list[str], keyTime: list[str]) -> list[str]:
    nameToMinutes = collections.defaultdict(list)

    for name, time in zip(keyName, keyTime):
      minutes = self._getMinutes(time)
      nameToMinutes[name].append(minutes)

    return sorted([name for name, minutes in nameToMinutes.items()
                   if self._hasAlert(minutes)])

  def _hasAlert(self, minutes: list[int]) -> bool:
    if len(minutes) > 70:
      return True
    minutes.sort()
    for i in range(2, len(minutes)):
      if minutes[i - 2] + 60 >= minutes[i]:
        return True
    return False

  def _getMinutes(self, time: str) -> int:
    h, m = map(int, time.split(':'))
    return 60 * h + m
# code by PROGIEZ

Additional Resources

See also  6. Zigzag Conversion LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.