Skip to content
Decoder

My Calendar II

Medium

Problem

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.

Example 1

cal = MyCalendarTwo()

Example 2

Input
cal.book(10, 20)
Output
true

Example 3

Input
cal.book(50, 60)
Output
true

Example 4

Input
cal.book(10, 40)
Output
true
Why
double booked during 10 to 20

Example 5

Input
cal.book(5, 15)
Output
false
Why
10 to 15 would be triple booked

Example 6

Input
cal.book(5, 10)
Output
true

Example 7

Input
cal.book(25, 55)
Output
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.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.