LeetCode 2688: Find Active Users
Problem Description
Explanation:
To find active users, we can use a hashtable to store the count of each user's activity. Then, we can iterate through the hashtable to find the active users based on some criteria.
- Create a hashtable
activityMap
to store the count of each user's activity. - Iterate through the activity log and populate the
activityMap
. - Iterate through the
activityMap
to find active users based on the given criteria. - Return the list of active users.
Time Complexity:
- Populating the
activityMap
takes O(N) where N is the number of activities. - Finding active users takes O(M) where M is the number of unique users.
- Overall time complexity is O(N + M).
Space Complexity:
- The hashtable
activityMap
will take O(M) space where M is the number of unique users.
:
Solutions
import java.util.*;
class Solution {
public List<String> findActiveUsers(String[] logs, int threshold) {
Map<String, Integer> activityMap = new HashMap<>();
for (String log : logs) {
String[] logParts = log.split(" ");
String user1 = logParts[0];
String user2 = logParts[1];
activityMap.put(user1, activityMap.getOrDefault(user1, 0) + 1);
if (!user1.equals(user2)) {
activityMap.put(user2, activityMap.getOrDefault(user2, 0) + 1);
}
}
List<String> activeUsers = new ArrayList<>();
for (Map.Entry<String, Integer> entry : activityMap.entrySet()) {
if (entry.getValue() >= threshold) {
activeUsers.add(entry.getKey());
}
}
return activeUsers;
}
}
Loading editor...