You are given a list of user activity logs logs, where each log is [user_id, day], meaning that the user with the given user_id performed an action on the specified day.
You are also given an integer k. Return the top k most active users sorted by their activity count in descending order. If two users have the same activity count, the user with the smaller user_id comes first.
Each log entry counts as one action. A user may have multiple actions on the same day, and each one counts separately.
Function Signature
def top_k_active_users(logs: list[list[int]], k: int) -> list[int]:
Rules
-
A user's activity count is the number of log entries with that
user_id
. The
day
value does not change the count, and repeated entries for the same day all count.
-
Rank users by activity count, highest first, and break ties by
user_id
, smallest first. Return the
user_id
values of the first
k
users in that ranking, in ranking order.
-
k
never exceeds the number of distinct users, so the output always has exactly
k
ids.
Constraints
-
1 <= len(logs) <= 10^5
-
1 <= user_id <= 10^4
-
1 <= day <= 365
-
1 <= k <=
number of unique users
Examples
Input: logs = [[1,1],[2,1],[1,2],[3,1],[1,3],[2,2],[3,2],[3,3],[3,4]], k = 2
Output: [3,1]
User 1 has 3 actions, user 2 has 2 and user 3 has 4. The two most active users are user 3 (4 actions) and user 1 (3 actions), in that order.
Input: logs = [[5,1],[5,2],[3,1],[3,2],[7,1]], k = 3
Output: [3,5,7]
Users 5 and 3 both have 2 actions, so user 3 (smaller id) comes first. User 7 has 1 action and comes last.
Input: logs = [[4,1],[4,1],[2,1],[9,5],[2,3],[9,6]], k = 2
Output: [2,4]
User 4's two entries share day 1 and still count as 2 actions, so all three users are tied at 2. The tie-break orders them 2, 4, 9, and the cut at k = 2 leaves out user 9.