Problem 485155 · hard · Level 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"]
args: [[], [10, 25], [20, 40], [35, 50], [22, 30], [50, 60], [25, 35], [30, 32]]
Output: [None, True, True, True, False, True, True, False]
Explanation: [22, 30) would triple-cover 22..25 together with [10, 25) and [20, 40). [50, 60) only
touches [35, 50) at 50, which is not an overlap. [25, 35) lies between the double-covered stretches
[20, 25) and [35, 40), touching both but overlapping neither, so it fits; it doubles 25..35, so
[30, 32) is then rejected.

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…