Max Requests In Window
EasySliding Window · SalesForce
A server logs the integer time of each request in timestamps. The list is not sorted and may contain the same time more than once (several requests at the same instant).
A window starting at time t covers every request whose timestamp x satisfies t <= x <= t + window_size (both ends inclusive). t can be any integer.
Return the maximum number of requests covered by a single window. Return 0 if there are no requests.
Example 1
- Input:
- timestamps = [5, 1, 2, 9, 4, 10, 2], window_size = 3
- Output:
- 4
- Explanation
- Sorted, the times are
1, 2, 2, 4, 5, 9, 10. The window[1, 4]covers1, 2, 2, 4, four requests, and no window of length 3 covers five.
Example 2
- Input:
- timestamps = [10, 20, 30], window_size = 9
- Output:
- 1
- Explanation
- Consecutive requests are 10 apart, which is more than
9, so any window holds at most one.
Example 3
- Input:
- timestamps = [6, 6, 6, 8], window_size = 0
- Output:
- 3
- Explanation
- With
window_size = 0a window covers a single instant, and three requests share time6.
Constraints
- 0 ≤
timestamps.length≤ 105 - 0 ≤
timestamps[i]≤ 109 - 0 ≤
window_size≤ 109