Similar Problems
Similar Problems not available
My Calendar I - Leetcode Solution
Companies:
LeetCode: My Calendar I Leetcode Solution
Difficulty: Medium
Topics: design binary-search
Problem:
Design a calendar system to support booking of appointments and viewing of events in a calendar.
The system should have the following functionalities:
- Book an appointment on a specific date
- Cancel an existing appointment on a specific date
- Check if an appointment is available on a specific date and time range
- View all appointments within a specific date range
Constraints:
- The calendar is for one person only
- Appointments can only be booked during business hours (9:00 AM - 5:00 PM)
- Appointments can be booked in increments of 30 minutes
- The calendar should be able to handle a large number of bookings and cancellations
Solution:
To solve the My Calendar I problem on Leetcode, we need to design a calendar system with the functionalities mentioned in the problem statement.
One way to implement this is by using a data structure to store the appointments. One such data structure could be a hashmap where the keys are the dates and the values are the list of appointments for that date. For each appointment, we can store its start and end times.
Book an appointment on a specific date: To book an appointment on a specific date, we first check if there are any existing appointments for the given date and time range. If there are no existing appointments, we add the new appointment to the hashmap.
Cancel an existing appointment on a specific date: To cancel an existing appointment on a specific date, we first check if there are any existing appointments for the given date and time range. If there is an existing appointment for that range, we remove it from the hashmap.
Check if an appointment is available on a specific date and time range: To check if an appointment is available on a specific date and time range, we first check if there are any existing appointments for the given date and time range. If there are no existing appointments, the appointment slot is available.
View all appointments within a specific date range: To view all appointments within a specific date range, we iterate over all the dates within the given range and print out the appointments for each date.
Code:
Here's the Python code for the My Calendar I problem on Leetcode:
class MyCalendar:
def __init__(self):
self.calendar = {}
def book(self, start: int, end: int) -> bool:
for s, e in self.calendar:
if s < end and e > start:
return False
self.calendar[(start, end)] = True
return True
Example:
calendar = MyCalendar()
print(calendar.book(10, 20)) # True print(calendar.book(15, 25)) # False print(calendar.book(20, 30)) # True
In the above example, we create a new calendar object and book three appointments. The first appointment from 10:00 AM to 10:30 AM is available, the second appointment from 3:00 PM to 3:30 PM overlaps with the first appointment and hence is not available, and the third appointment from 8:00 PM to 8:30 PM is available.
My Calendar I Solution Code
1