A recording studio has two rooms, so two sessions may overlap, but never three. Design a class Studio:
Studio()starts with no bookings.book(start, end)requests the half-open time range[start, end)(soenditself is free). If adding it would make some moment covered by three accepted bookings, reject it: returnFalseand record nothing. Otherwise record it and returnTrue.
Examples
ops: ["Studio", "book", "book", "book", "book", "book", "book", "book", "book"]
args: [[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55], [30, 52], [40, 50]]
Output: [None, True, True, True, False, True, True, False, True]
Explanation: [5, 15) would triple-cover 10..15. [5, 10) only touches [10, 20) at 10, which is not an overlap.
[30, 52) would triple-cover 30..40 together with [10, 40) and [25, 55).
Constraints
0 <= start < end <= 10**9- Up to
1000calls tobook - Target:
O(n)perbook, wherenis the number of accepted bookings
Goals
- Keep the accepted bookings and, separately, the stretches already covered twice
- Test a new interval against the double-covered stretches before accepting it
- Use half-open intervals consistently