You are given a list of timestamped events and a list of queries. Each event is represented as [time, event_id]. Each query is represented as [type, start_time, end_time], where type indicates the operation:
-
type = 0
: return the number of events whose time falls within
[start_time, end_time]
(inclusive).
-
type = 1
: return the
event_id
of the most frequent event within
[start_time, end_time]
(inclusive). If multiple events share the highest frequency, return the smallest
event_id
. If there are no events in the range, return
-1
.
Return an array containing the result of each query in the order the queries are given.
Function Signature
def process_queries(events: list[list[int]], queries: list[list[int]]) -> list[int]:
Rules
-
Both ends of a query range are inclusive: an event at exactly
start_time
or
end_time
is in the range.
-
The frequency of an
event_id
in a range is the number of events in that range carrying that id.
-
Events are not guaranteed to be sorted by time. Every event counts once, including events that share a time with another event.
-
A
type = 0
query over a range with no events returns
0
. A
type = 1
query over such a range returns
-1
.
Constraints
-
1 <= len(events) <= 10^4
-
1 <= len(queries) <= 10^4
-
1 <= start_time <= end_time <= 10^9
-
Each event is
[time, event_id]
with
1 <= event_id <= 10^5
-
Each query is
[type, start_time, end_time]
, where
type
is
0
(count) or
1
(most frequent)
Examples
Input: events = [[1, 10], [2, 20], [3, 10], [4, 30], [5, 10], [6, 20]], queries = [[0, 1, 6], [1, 1, 6], [0, 2, 4], [1, 3, 5]]
Output: [6, 10, 3, 10]
Query [0, 1, 6] counts all 6 events. In [1, 6] event 10 appears 3 times, more than 20 (twice) or 30 (once). Times 2, 3 and 4 give a count of 3, and in [3, 5] event 10 appears twice against one 30.
Input: events = [[5, 1], [10, 2], [15, 1], [20, 3], [25, 1]], queries = [[1, 1, 30], [0, 10, 20], [1, 11, 19]]
Output: [1, 3, 1]
Event 1 appears 3 times overall, so it is the most frequent. The events at times 10, 15 and 20 fall in [10, 20], including both endpoints. Only event 1 (at time 15) falls in [11, 19].
Input: events = [[3, 7], [1, 5], [2, 7], [4, 5], [9, 2]], queries = [[1, 1, 4], [1, 5, 8], [0, 5, 8], [1, 9, 9]]
Output: [5, -1, 0, 2]
In [1, 4] ids 5 and 7 each appear twice, so the smaller id 5 wins. No event has a time from 5 to 8, which gives -1 for the most frequent query and 0 for the count. The range [9, 9] holds only event 2.