Calendar 3

Design a calendar system that keeps track of the maximum number of overlapping events. A k-booking happens when k events have some non-empty intersection (i.e., there is some time that is common to all k events).

The events are represented as half-open intervals [startTime, endTime), meaning the event starts at startTime and ends right before endTime.

Implement the MyCalendarThree class:

  • MyCalendarThree(): Initializes the object.
  • int book(int startTime, int endTime): Adds the event to the calendar and returns an integer k representing the maximum k-booking across all events added to the calendar so far.

Example 1:

Input: ["MyCalendarThree", "book", "book", "book", "book"]

[[], [10, 20], [15, 25], [18, 22], [5, 12]]

Output: [null, 1, 2, 3, 3]

Explanation: The first booking establishes a maximum overlap of 1. The second booking overlaps the first, pushing the maximum concurrent events to 2. The third booking [18, 22) intersects with both existing events simultaneously between time 18 and 20, raising the global maximum overlap to 3. The final booking [5, 12) only overlaps with the very beginning of the first event, creating a localized double-booking, but leaving the overall global maximum overlap unchanged at 3.

Example 2:

Input: ["MyCalendarThree", "book", "book", "book", "book", "book"]

[[], [20, 30], [25, 35], [40, 50], [45, 55], [28, 48]]

Output: [null, 1, 2, 2, 2, 3]

Explanation: We first establish two completely isolated clusters: one between times 20 and 35, and another between times 40 and 55. Within each cluster, the events overlap exactly once, maintaining a global maximum k-booking of 2 for the first four requests. The final massive booking [28, 48) acts as a bridge, slicing through both clusters. It overlaps the double-booked region in the first cluster (from time 28 to 30) and the double-booked region in the second cluster (from time 45 to 48), successfully pushing the global maximum concurrent events to 3.

Now Your Turn!

Pick the correct output for the given input

Input: ["MyCalendarThree", "book", "book", "book", "book"]

[[], [10, 100], [20, 90], [30, 80], [40, 70]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= startTime < endTime <= 109
  • At most 400 calls will be made to book.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class MyCalendarThree {
public:
MyCalendarThree() {
}
int book(int startTime, int endTime) {
}
};
Test Case

Input:

Methods
Params