You are given an array of event timestamps events, sorted in non-decreasing order, and an integer window_size.
Return the maximum number of events that occur within any contiguous time window of length window_size.
A window starting at time t covers the half-open interval [t, t + window_size), meaning an event with timestamp exactly equal to t + window_size is not included in the window.
Function Signature
def max_events_in_window(events: list[int], window_size: int) -> int:
Rules
-
The start time
t
can be any value. It does not have to be one of the timestamps.
-
Every element of
events
is a separate event, so repeated timestamps each count.
Constraints
-
1 <= len(events) <= 10^5
-
0 <= events[i] <= 10^9
-
events
is sorted in non-decreasing order.
-
1 <= window_size <= 10^9
Examples
Input: events = [1, 2, 3, 7, 8, 10, 15], window_size = 5
Output: 3
The window [3, 8) holds only the events at 3 and 7, because 8 is excluded. The window [7, 12) holds 7, 8 and 10, which is 3 events and the maximum (the window [1, 6) also reaches 3).
Input: events = [0, 0, 0, 5, 5, 10], window_size = 1
Output: 3
The window [0, 1) contains the three events at time 0. No other window of size 1 contains more.
Input: events = [1, 6], window_size = 5
Output: 1
A window that contains time 1 must start at or before 1, so it ends at or before 6, and the event at 6 falls on the excluded endpoint. No window of length 5 holds both events.