The Indian Engineer

Problem 731 My Calendar II

Problem Statement

Link - Problem 731

Question

You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking.

A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).

The event can be represented as a pair of integers startTime and endTime that represents a booking on the half-open interval [startTime, endTime), the range of real numbers x such that startTime <= x < endTime.

Implement the MyCalendarTwo class:

Example 1:

Input
["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
Output
[null, true, true, true, false, true, true]

Explanation
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
myCalendarTwo.book(10, 20); // return True, The event can be booked. 
myCalendarTwo.book(50, 60); // return True, The event can be booked. 
myCalendarTwo.book(10, 40); // return True, The event can be double booked. 
myCalendarTwo.book(5, 15);  // return False, The event cannot be booked, 
 because it would result in a triple booking.
myCalendarTwo.book(5, 10); // return True, The event can be booked, 
 as it does not use time 10 which is already double booked.
myCalendarTwo.book(25, 55); // return True, The event can be booked, 
 as the time in [25, 40) will be double booked with the third event, 
 the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.

Constraints:

Solution

class MyCalendarTwo {
public:
    map<int, int> bookingCount;
    int maxOverlappedBooking;

    MyCalendarTwo() { maxOverlappedBooking = 2; }

    bool book(int start, int end) {
        bookingCount[start]++;
        bookingCount[end]--;

        int overlappedBooking = 0;
        for (pair<int, int> bookings : bookingCount) {
            overlappedBooking += bookings.second;
            if (overlappedBooking > maxOverlappedBooking) {
                bookingCount[start]--;
                bookingCount[end]++;

                if (bookingCount[start] == 0) {
                    bookingCount.erase(start);
                }
                if (bookingCount[end] == 0) {
                    bookingCount.erase(end);
                }

                return false;
            }
        }

        return true;
    }
};

/**
 * Your MyCalendarTwo object will be instantiated and called as such:
 * MyCalendarTwo* obj = new MyCalendarTwo();
 * bool param_1 = obj->book(start,end);
 */

Complexity Analysis

| Algorithm            | Time Complexity | Space Complexity |
| -------------------- | --------------- | ---------------- |
| Sweep line algorithm | O(n)            | O(n)             |

Explanation

Intial Thoughts

To approach this problem, we need to think about how to efficiently track bookings and overlaps. We could use a data structure that allows us to store and update bookings easily, such as a map or an array. Key considerations include how to identify overlaps, how to handle edge cases like booking start and end times, and how to keep track of booking counts. Main points to consider are: identifying the right data structure, understanding how to calculate overlaps, determining how to handle edge cases, thinking about how to optimize the solution for performance, and considering how to implement the booking count logic.

Intuitive Analysis

Intuitively, we can solve this problem by visualizing the calendar as a series of lines representing bookings. When a new booking is added, we check for overlaps by looking for any existing bookings that share a common time slot. A key insight is to recognize that a triple booking occurs when three lines overlap at the same point. To intuitively solve this, we need to: consider using a sweep line approach to track overlaps, think about how to update the booking count as new bookings are added, recognize the importance of handling edge cases correctly, understand how to optimize the solution to avoid unnecessary checks, and think about how to use the data structure to efficiently track and update booking information.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Store two sorted lists of intervals: one list will be all times that are at least single booked, and another list will be all times that are definitely double booked. If none of the double bookings conflict, then the booking will succeed, and you should update your single and double bookings accordingly.


Similar Questions:

Title URL Difficulty
My Calendar I https://leetcode.com/problems/my-calendar-i Medium
My Calendar III https://leetcode.com/problems/my-calendar-iii Hard