Skip to content
Decoder

My Calendar III

Hard

Problem

Build a MyCalendarThree class. Every call to book(start, end) adds the half-open event [start, end); bookings are never rejected. After each booking, return the largest k such that some moment is covered by k events, counting every event booked so far.

Example 1

cal = MyCalendarThree()

Example 2

Input
cal.book(10, 20)
Output
1

Example 3

Input
cal.book(50, 60)
Output
1

Example 4

Input
cal.book(10, 40)
Output
2

Example 5

Input
cal.book(5, 15)
Output
3
Why
10 to 15 is covered three times

Example 6

Input
cal.book(5, 10)
Output
3

Example 7

Input
cal.book(25, 55)
Output
3

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

Constraints

  • 0 <= start < end <= 10^9
  • At most 400 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.