1384. Busiest Camera Window
A wildlife camera can be switched on once. Event i happens during the closed time range [starts[i], starts[i] + lengths[i]]. If the camera is switched on at any real moment t, it records the closed window [t, t + width] and catches an event whenever the window and the event share at least one instant, even a single shared point.
Return the largest number of events one window can catch. You may choose t freely, including negative or fractional values. Note that the window catches event i exactly when t lies in the closed range [starts[i] - width, starts[i] + lengths[i]], so the task is to find the point covered by the most of these shifted ranges. Closed ranges that touch at one point both count there, so at equal coordinates process starts before ends.
Coordinates can exceed 32 bits, so use 64-bit arithmetic. The intended solution runs in O(n log n) time and O(n) space.
Example 1
- Input:
- starts = [0,10,4], lengths = [2,1,3], width = 3
- Output:
- 2
- Explanation:
The events are [0,2], [10,11] and [4,7]; the window [7,10] touches [4,7] at 7 and [10,11] at 10, and no window can catch all three.
Example 2
- Input:
- starts = [5,5,5], lengths = [0,0,0], width = 0
- Output:
- 3
- Explanation:
Three events occupy the single instant 5, and a window of width 0 placed at 5 catches all of them.
Example 3
- Input:
- starts = [1,20], lengths = [1,1], width = 5
- Output:
- 1
- Explanation:
The events [1,2] and [20,21] are 18 apart, more than the width, so only one fits in a window.
Constraints
1 ≤ n ≤ 105
starts.length == lengths.length == n
0 ≤ starts[i], lengths[i], width ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 6,000 msC++ 1,500 msJava 3,000 msJavaScript 3,000 msTypeScript 3,000 ms
Expected complexity
- Time
- O(n log n)
- Space
- O(n)