LeetCode 2688: Find Active Users Solution
Master LeetCode problem 2688 (Find Active Users), 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.
2688. Find Active Users
Problem Explanation
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
activityMapto store the count of each user's activity. - Iterate through the activity log and populate the
activityMap. - Iterate through the
activityMapto find active users based on the given criteria. - Return the list of active users.
Time Complexity:
- Populating the
activityMaptakes 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
activityMapwill take O(M) space where M is the number of unique users.
:
Solution Code
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;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2688 (Find Active Users)?
This page provides optimized solutions for LeetCode problem 2688 (Find Active Users) 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 2688 (Find Active Users)?
The time complexity for LeetCode 2688 (Find Active Users) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2688 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2688 in Java, C++, or Python.