Solving Leetcode Interviews in Seconds with AI: My Calendar I
Introduction
In this blog post, we will explore how to solve the LeetCode problem "729" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
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: MyCalendar() Initializes the calendar object. boolean book(int startTime, int endTime) Returns true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar. Example 1: Input ["MyCalendar", "book", "book", "book"] [[], [10, 20], [15, 25], [20, 30]] Output [null, true, false, true] Explanation MyCalendar myCalendar = new MyCalendar(); myCalendar.book(10, 20); // return True myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event. myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20. Constraints: 0 <= start < end <= 109 At most 1000 calls will be made to book.
Explanation
Here's an efficient solution for the calendar booking problem:
- Binary Search Tree (BST): Store the booked events as nodes in a BST. Each node represents an event with its start and end times.
- Overlapping Interval Check: Before booking a new event, traverse the BST to find any overlapping events. If an overlap is found, the booking fails.
BST Insertion: If no overlap is found, insert the new event into the BST while maintaining the BST properties (sorted by start time).
Runtime Complexity: O(N log N) for N book calls (due to BST operations).
- Storage Complexity: O(N) to store the booked events in the BST.
Code
class Node:
def __init__(self, start, end):
self.start = start
self.end = end
self.left = None
self.right = None
class MyCalendar:
def __init__(self):
self.root = None
def book(self, start, end):
if not self.root:
self.root = Node(start, end)
return True
return self._insert(self.root, start, end)
def _insert(self, node, start, end):
if start >= node.end: # No overlap, new event is completely after the current node's event
if node.right:
return self._insert(node.right, start, end)
else:
node.right = Node(start, end)
return True
elif end <= node.start: # No overlap, new event is completely before the current node's event
if node.left:
return self._insert(node.left, start, end)
else:
node.left = Node(start, end)
return True
else: # Overlap found
return False