1385. Solo Coverage
A city assigns radio channels along a long highway. Booking i reserves the half-open stretch [starts[i], starts[i] + lengths[i]), and bookings may overlap each other. A piece of highway is exclusive to booking i if booking i covers it and no other booking does.
Return an array solo of length n where solo[i] is the total length of highway that is covered by booking i and by no other booking. Two identical bookings therefore both get 0 for the stretch they share. Totals can exceed 32 bits, so return 64-bit integers.
Sort the start and end boundaries, sweep through them while keeping the number of active bookings and the sum of the indices of the active bookings. Whenever exactly one booking is active, that sum is the index of the lone booking and the stretch up to the next boundary is credited to it. The intended solution runs in O(n log n) time and O(n) space.
Example 1
- Input:
- starts = [0,3,10], lengths = [5,5,2]
- Output:
- [3,3,2]
- Explanation:
Booking 0 is [0,5), booking 1 is [3,8) and booking 2 is [10,12); booking 0 alone holds [0,3), booking 1 alone holds [5,8) and booking 2 holds all of its 2 units.
Example 2
- Input:
- starts = [4,4], lengths = [6,6]
- Output:
- [0,0]
- Explanation:
The two bookings are identical, so no stretch belongs to just one of them.
Example 3
- Input:
- starts = [0], lengths = [1000000000]
- Output:
- [1000000000]
- Explanation:
A single booking owns its whole length.
Constraints
1 ≤ n ≤ 105
starts.length == lengths.length == n
0 ≤ starts[i] ≤ 109
1 ≤ lengths[i] ≤ 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)