2933. High-Access Employees LeetCode Solution

In this guide, you will get 2933. High-Access Employees LeetCode Solution with the best time and space complexity. The solution to High-Access Employees 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. High-Access Employees solution in C++
  4. High-Access Employees solution in Java
  5. High-Access Employees solution in Python
  6. Additional Resources
2933. High-Access Employees LeetCode Solution image

Problem Statement of High-Access Employees

You are given a 2D 0-indexed array of strings, access_times, with size n. For each i where 0 <= i <= n – 1, access_times[i][0] represents the name of an employee, and access_times[i][1] represents the access time of that employee. All entries in access_times are within the same day.
The access time is represented as four digits using a 24-hour time format, for example, "0800" or "2250".
An employee is said to be high-access if he has accessed the system three or more times within a one-hour period.
Times with exactly one hour of difference are not considered part of the same one-hour period. For example, "0815" and "0915" are not part of the same one-hour period.
Access times at the start and end of the day are not counted within the same one-hour period. For example, "0005" and "2350" are not part of the same one-hour period.
Return a list that contains the names of high-access employees with any order you want.

Example 1:

See also  1971. Find if Path Exists in Graph LeetCode Solution

Input: access_times = [[“a”,”0549″],[“b”,”0457″],[“a”,”0532″],[“a”,”0621″],[“b”,”0540″]]
Output: [“a”]
Explanation: “a” has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21.
But “b” does not have more than two access times at all.
So the answer is [“a”].
Example 2:

Input: access_times = [[“d”,”0002″],[“c”,”0808″],[“c”,”0829″],[“e”,”0215″],[“d”,”1508″],[“d”,”1444″],[“d”,”1410″],[“c”,”0809″]]
Output: [“c”,”d”]
Explanation: “c” has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29.
“d” has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08.
However, “e” has just one access time, so it can not be in the answer and the final answer is [“c”,”d”].
Example 3:

Input: access_times = [[“cd”,”1025″],[“ab”,”1025″],[“cd”,”1046″],[“cd”,”1055″],[“ab”,”1124″],[“ab”,”1120″]]
Output: [“ab”,”cd”]
Explanation: “ab” has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24.
“cd” has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55.
So the answer is [“ab”,”cd”].

Constraints:

1 <= access_times.length <= 100
access_times[i].length == 2
1 <= access_times[i][0].length <= 10
access_times[i][0] consists only of English small letters.
access_times[i][1].length == 4
access_times[i][1] is in 24-hour time format.
access_times[i][1] consists only of '0' to '9'.

Complexity Analysis

  • Time Complexity: O(|\texttt{sort}|)
  • Space Complexity: O(n)

2933. High-Access Employees LeetCode Solution in C++

class Solution {
 public:
  vector<string> findHighAccessEmployees(vector<vector<string>>& access_times) {
    unordered_set<string> ans;

    ranges::sort(access_times);

    for (int i = 0; i + 2 < access_times.size(); ++i) {
      const string& name = access_times[i][0];
      if (ans.contains(name))
        continue;
      if (name != access_times[i + 2][0])
        continue;
      if (stoi(access_times[i + 2][1]) - stoi(access_times[i][1]) < 100)
        ans.insert(name);
    }

    return {ans.begin(), ans.end()};
  }
};
/* code provided by PROGIEZ */

2933. High-Access Employees LeetCode Solution in Java

class Solution {
  public List<String> findHighAccessEmployees(List<List<String>> access_times) {
    Set<String> ans = new HashSet<>();

    Collections.sort(access_times,
                     (a, b)
                         -> a.get(0).equals(b.get(0)) ? a.get(1).compareTo(b.get(1))
                                                      : a.get(0).compareTo(b.get(0)));

    for (int i = 0; i + 2 < access_times.size(); ++i) {
      String name = access_times.get(i).get(0);
      if (ans.contains(name))
        continue;
      if (!name.equals(access_times.get(i + 2).get(0)))
        continue;
      if (Integer.parseInt(access_times.get(i + 2).get(1)) -
              Integer.parseInt(access_times.get(i).get(1)) <
)
        ans.add(name);
    }

    return new ArrayList<>(ans);
  }
}
// code provided by PROGIEZ

2933. High-Access Employees LeetCode Solution in Python

class Solution:
  def findHighAccessEmployees(self, access_times: list[list[str]]) -> list[str]:
    ans = set()

    access_times.sort()

    for i in range(len(access_times) - 2):
      name = access_times[i][0]
      if name in ans:
        continue
      if name != access_times[i + 2][0]:
        continue
      if int(access_times[i + 2][1]) - int(access_times[i][1]) < 100:
        ans.add(name)

    return list(ans)
# code by PROGIEZ

Additional Resources

See also  197. Rising Temperature LeetCode Solution

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