You are given two log files, log1 and log2, each represented as a list of entries. Each entry is a list of three integers, [customer_id, article_id, timestamp], meaning a customer accessed an article at a given time.
log1 holds the access records from Day 1 and log2 holds the access records from Day 2. A customer may appear several times in the same log file (accessing different articles, or the same article at different times).
Return a sorted list of the unique customer IDs that accessed articles on both days. Aim for a running time close to linear in the total number of entries.
Function Signature
def find_common_customers(log1: list[list[int]], log2: list[list[int]]) -> list[int]:
Rules
-
Each entry is
[customer_id, article_id, timestamp]
. Only
customer_id
decides the answer; article IDs and timestamps do not affect it.
-
Each customer ID appears at most once in the output, however many times it shows up in either log.
-
Return the IDs sorted in ascending order. Return an empty list when no customer appears in both logs.
Constraints
-
1 <= len(log1), len(log2) <= 10^5
-
1 <= customer_id <= 10^6
-
1 <= article_id <= 10^6
-
0 <= timestamp <= 10^9
Examples
Input: log1 = [[1,101,1000],[2,102,1001],[3,103,1002],[1,104,1003]], log2 = [[2,201,2000],[3,202,2001],[4,203,2002],[3,204,2003]]
Output: [2,3]
Customer 1 appears only in log1 and customer 4 appears only in log2. Customers 2 and 3 appear in both log files, so the answer is [2,3].
Input: log1 = [[5,100,500],[6,101,501]], log2 = [[7,200,600],[8,201,601]]
Output: []
No customer appears in both log files, so the answer is an empty list.
Input: log1 = [[9,1,10],[4,2,20],[9,3,30]], log2 = [[9,7,40],[4,7,41],[9,8,42]]
Output: [4,9]
Customer 9 shows up twice in each log but is listed once. The output is in ascending order even though 9 comes before 4 in both logs.