Shared Exposure Windows
HardOnline Assessment
Count the start times at which an observatory array can begin a long exposure that keeps enough telescopes free throughout.
A night lasts length minutes, numbered 0 to length - 1. A long exposure takes window consecutive minutes: started at minute t, it occupies minutes t to t + window - 1, and must finish within the night, so 0 <= t <= length - window. It needs k different telescopes, and each of them has to be free for the whole exposure (a telescope that is free for the first half and a different one for the second half do not count together).
Each telescope has a list of maintenance bookings [start, end): it is busy during minutes start to end - 1, so a booking that ends at end leaves minute end free, and a booking with start = end is empty. Bookings of one telescope may overlap, nest, touch each other, and come in any order.
An exposure starting at t is possible if at least k telescopes have no busy minute among t .. t + window - 1.
Input: length, the minutes in the night; window, the exposure length; k, the number of telescopes needed; bookings, one list of [start, end] pairs per telescope (0 <= start <= end <= length).
Output: the number of start times t at which an exposure is possible. If window > length or fewer than k telescopes exist, the answer is 0.
Example 1
- Input:
- [14,3,2,[[[5,7]],[[4,9],[3,8]],[[9,11]]]]
- Output:
- 6
- Explanation
- The exposure lasts 3 minutes, so t ranges over 0..11. Telescope 0 (busy 5-6) is free for t in 0..2 and 7..11. Telescope 1's overlapping bookings merge into busy 3-8, so it is free for t = 0 and 9..11. Telescope 2 (busy 9-10) is free for t in 0..6 and 11. At least two are free for t = 0, 1, 2, 9, 10, 11: six start times. For t = 3 no pair works, even though minutes 3 to 5 each have two free telescopes at some point.
Example 2
- Input:
- [10,4,2,[[[2,3]],[[0,4],[4,6]],[[6,10]]]]
- Output:
- 1
- Explanation
- Telescope 0 is free for t in 3..6, telescope 1 (the touching bookings make one busy block 0-5) only for t = 6, and telescope 2 (busy 6-9) for t in 0..2. Only t = 6 has two free telescopes: the exposure covers minutes 6 to 9, starting the minute after telescope 1's booking ends.
Example 3
- Input:
- [1000000000,300000000,2,[[[200000000,250000000]],[[500000000,600000000]],[[0,100000000],[900000000,1000000000]]]]
- Output:
- 550000002
- Explanation
- Telescope 0 is free for t in [250000000, 700000000], telescope 1 for [0, 200000000] and [600000000, 700000000], telescope 2 for [100000000, 600000000]. Two or more are free on [100000000, 200000000] and [250000000, 700000000]: 100000001 + 450000001 = 550000002 start times.
Constraints
- 1 ≤ length ≤ 109; 1 ≤ window ≤ 109; 1 ≤ k ≤ 50
- 0 ≤ bookings.length ≤ 40; each telescope has at most 40 bookings
- 0 ≤ start ≤ end ≤ length