You are given interaction logs for pins. Each log [pin_id, start, end] means that some user was interacting with pin pin_id during the half-open time interval [start, end). For every elementary time segment defined by the logs, report how many distinct pins were being interacted with.
The same pin can appear in many logs, because several users can interact with it, and its intervals can overlap. A pin counts once in a segment, no matter how many of its intervals cover that segment.
This is the last step of a three-step progression. The first version limited timestamps to a 60-second window (0 to 59) and assumed each pin appeared in at most one log. The second version removed the window limit, allowing arbitrary timestamps. This version also removes the one-log-per-pin assumption.
Function Signature
def distinct_active_pins(logs: list[list[int]]) -> list[list[int]]:
...
Rules
-
Let
T
be the sorted list of distinct values that appear as a
start
or an
end
in any log.
-
For each pair of consecutive values
T[i] < T[i + 1]
, output one row
[T[i], T[i + 1], count]
. Here
count
is the number of distinct
pin_id
values that have at least one log with
start <= T[i]
and
T[i + 1] <= end
.
-
Output the rows in ascending order of their start time. Output a row for every consecutive pair, including rows whose count is
0
and neighboring rows with equal counts. Do not merge rows.
-
Intervals are half-open. A log that ends at time
t
and another that starts at time
t
do not overlap.
-
If
logs
is empty, return an empty list.
Constraints
-
0 <= len(logs) <= 100000
-
0 <= pin_id <= 10^9
-
0 <= start < end <= 10^12
-
Timestamps can exceed
2^31 - 1
, so use 64-bit integers.
-
Logs are unsorted, and some may be exact duplicates of each other.
Examples
Input: logs = [[7, 1, 5], [7, 3, 8], [9, 4, 6]]
Output: [[1, 3, 1], [3, 4, 1], [4, 5, 2], [5, 6, 2], [6, 8, 1]]
During [3, 5), two of pin 7's logs overlap, but pin 7 is counted only once.
Input: logs = [[1, 0, 2], [2, 5, 6]]
Output: [[0, 2, 1], [2, 5, 0], [5, 6, 1]]