Skip to content
BytePatterns

Calendar Without Double Booking

MediumIntervals#sorted-intervals#bisect-insert#half-open-intervals~25m

Problem

A meeting room accepts booking requests one at a time. Each request is a half-open span [start, end), so a booking ending at 20 does not clash with one starting at 20. book(start, end) must add the span and return True if it overlaps no accepted booking, and otherwise leave the calendar unchanged and return False. There are up to 1,000 requests and 0 ≤ start < end ≤ 10^9. Keep the accepted bookings sorted so that each request is checked against only its neighbours.

Examples

Input:  book(10, 20), book(15, 25), book(20, 30)
Output: [True, False, True]
Why:    [15, 25) overlaps [10, 20); [20, 30) only touches it
Input:  book(5, 10), book(1, 5), book(3, 4)
Output: [True, True, False]
Why:    [3, 4) falls inside [1, 5)
Input:  book(1, 2), book(1, 2)
Output: [True, False]
Why:    edge case, the same span twice

Hints

0 / 3

Stuck on the idea rather than the code? Insert Interval covers it.