Advertisement

My Calendar I - LeetCode 729 Solution

My Calendar I - Complete Solution Guide

My Calendar I is LeetCode problem 729, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking . A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both 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 MyCalendar class: M

Detailed Explanation

The problem is to design a calendar that prevents double bookings. A double booking occurs when two events have overlapping time intervals. The `MyCalendar` class should have a `book` method that takes a start time and an end time as input and returns `true` if the event can be added to the calendar without causing a double booking. Otherwise, it returns `false` and does not add the event. The time intervals are half-open intervals, meaning they include the start time but exclude the end time (e.g., [10, 20) includes 10 up to, but not including, 20). The constraints are 0 <= start < end <= 10^9 and at most 1000 calls to book.

Solution Approach

The solution uses a sorted list (or vector in C++) to store the events in the calendar. When a new event is to be booked, a binary search is performed to find the correct position to insert the new event, maintaining sorted order. Before inserting, the algorithm checks for overlap between the new event and the existing events directly before and after the insertion point. If no overlap is found, the new event is inserted into the calendar, and `true` is returned. Otherwise, `false` is returned without modifying the calendar.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty list (or vector) called `calendar` to store the events. Each event will be represented as a pair of integers: [startTime, endTime].
  2. Step 2: When the `book(startTime, endTime)` method is called, use binary search (or `bisect_left` in Python, `lower_bound` in C++) to find the index `idx` where the new event should be inserted to maintain the sorted order of start times.
  3. Step 3: Check for overlap with the event immediately before the insertion point (if it exists). If `idx > 0`, check if `calendar[idx - 1][1] > startTime`. If true, there's an overlap, so return `false`.
  4. Step 4: Check for overlap with the event immediately after the insertion point (if it exists). If `idx < len(calendar)`, check if `calendar[idx][0] < endTime`. If true, there's an overlap, so return `false`.
  5. Step 5: If no overlaps are found in steps 3 and 4, insert the new event [startTime, endTime] at index `idx` in the `calendar`.
  6. Step 6: Return `true` to indicate that the event was successfully booked.

Key Insights

  • Insight 1: The calendar needs to maintain the events in sorted order by start time to efficiently check for overlaps.
  • Insight 2: Binary search is an efficient way to find the correct insertion point for a new event while maintaining the sorted order.
  • Insight 3: To check for overlaps, it's sufficient to compare the new event with its immediate neighbors (the event before and the event after) in the sorted calendar.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Binary Search, Design, Segment Tree, Ordered Set.

Companies

Asked at: Flexport.