Skip to content

My Calendar II

MediumIntervals
Decode it first. Clues, constraints, pattern and plan before any code.

Build a MyCalendarTwo class. book(start, end) tries to add the half-open event [start, end). Double bookings are fine, but a triple booking (some moment covered by three events) is not. If the new event would cause a triple booking, return false and do not store it. Otherwise store it and return true.

cal = MyCalendarTwo()
cal.book(10, 20)  →  true
cal.book(50, 60)  →  true
cal.book(10, 40)  →  true    (double booked during 10 to 20)
cal.book(5, 15)   →  false   (10 to 15 would be triple booked)
cal.book(5, 10)   →  true
cal.book(25, 55)  →  true

The TypeScript class has the same shape: new MyCalendarTwo() and cal.book(start, end).

Constraints

  • 0 <= start < end <= 10^9
  • At most 1000 calls to book.