Problem 485155 · hard · Phase 04 Non-Linear Data Structures

Studio Bookings Without Triple Overlap

design · intervals · classes

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) (so end itself is free). If adding it would make some moment covered by three accepted bookings, reject it: return False and record nothing. Otherwise record it and return True.

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 1000 calls to book
  • Target: O(n) per book, where n is 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
Starting Python…