You are given a log of messages on a messaging platform. Each message is [sender_id, receiver_id, timestamp]: the user who sent it, the user who received it, and the time it was sent in seconds.
A user interacts with another user if they sent a message to that user or received a message from that user. Given a reference_time and an integer x, find every user who interacted with more than x unique people during the last 24 hours, meaning messages with timestamps in the range (reference_time - 86400, reference_time].
Return the IDs of these power users sorted in ascending order. If no user qualifies, return an empty list.
Function Signature
def find_power_users(messages: list[list[int]], reference_time: int, x: int) -> list[int]:
Rules
-
Each message is a list of three integers
[sender_id, receiver_id, timestamp]
.
-
The window is half-open. A message at exactly
reference_time - 86400
is outside it, a message at exactly
reference_time
is inside it, and messages after
reference_time
are outside it.
-
An interaction has no direction. Sending a message to user B and receiving one from user B both make B a partner, and several messages with the same partner count as one.
-
Return each qualifying user ID once, in ascending order.
Constraints
-
1 <= len(messages) <= 10^5
-
1 <= sender_id, receiver_id <= 10^6
-
sender_id != receiver_id
-
0 <= timestamp <= 10^18
-
0 <= reference_time <= 10^18
-
1 <= x <= 10^5
Examples
Input: messages = [[1,2,100],[1,3,200],[1,4,300],[2,3,150],[5,1,250]], reference_time = 1000, x = 2
Output: [1]
The window is (-85400, 1000], so every message counts. User 1 interacted with 2, 3, 4 and 5, which is 4 people. Users 2 and 3 each interacted with 2 people, which is not more than 2, and users 4 and 5 with 1.
Input: messages = [[10,20,1000],[10,30,2000],[10,40,3000],[20,30,1500],[20,40,2500],[20,50,3500],[40,10,3800],[30,50,4500]], reference_time = 4000, x = 2
Output: [10, 20]
The message at 4500 comes after reference_time, so it is ignored. User 10 interacted with 20, 30 and 40 (the two messages with 40 count once) and user 20 with 10, 30, 40 and 50, while users 30 and 40 have 2 partners each and user 50 has 1.
Input: messages = [[1,2,100],[1,3,200],[1,4,300],[2,3,150],[5,1,250]], reference_time = 86600, x = 2
Output: []
The window is (200, 86600]. Only the messages at 250 and 300 fall inside it, because the message at exactly 200 sits on the open end. User 1 is left with partners 4 and 5, which is not more than 2.