# Solution For My Calendar I

Problem:

Design a calendar system to support booking of appointments and viewing of events in a calendar.

The system should have the following functionalities:

1. Book an appointment on a specific date
2. Cancel an existing appointment on a specific date
3. Check if an appointment is available on a specific date and time range
4. View all appointments within a specific date range

Constraints:
1. The calendar is for one person only
2. Appointments can only be booked during business hours (9:00 AM – 5:00 PM)
3. Appointments can be booked in increments of 30 minutes
4. 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.

## Step by Step Implementation For My Calendar I

```/**
* MyCalendar class will be used to book events and track whether the event can be booked or not
*/
public class MyCalendar {
private List calendar;

public MyCalendar() {
calendar = new ArrayList<>();
}

/**
* This method is used to book an event
* @param start start time of the event
* @param end end time of the event
* @return true if the event can be booked, false otherwise
*/
public boolean book(int start, int end) {
for (int[] event : calendar) {
// if the start or end time of the new event falls within the start and end time of an existing event, return false
if (start >= event[0] && start < event[1] || end > event[0] && end <= event[1] || start <= event[0] && end >= event[1]) {
return false;
}
}
// otherwise, the event can be booked and we add it to our calendar
return true;
}
}```
```class MyCalendar(object):
def __init__(self):
self.calendar = []

def book(self, start, end):
for i, j in self.calendar:
if start < j and end > i:
return False
self.calendar.append((start, end))
return True```
```var MyCalendar = function() {
this.calendar = [];
};

/**
* @param {number} start
* @param {number} end
* @return {boolean}
*/
MyCalendar.prototype.book = function(start, end) {
for (let i = 0; i < this.calendar.length; i++) {
const [s, e] = this.calendar[i];
if (Math.max(s, start) < Math.min(e, end)) {
return false;
}
}
this.calendar.push([start, end]);
return true;
};

/**
* Your MyCalendar object will be instantiated and called as such:
* var obj = Object.create(MyCalendar).createNew()
* var param_1 = obj.book(start,end)
*/```
```class MyCalendar {
public:
MyCalendar() {

}

bool book(int start, int end) {
for(auto& it : calendar){
if(it.second > start && it.first < end) return false;
}
calendar.emplace(end, start);
return true;
}
private:
map calendar; // sorted by end time
};

/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* bool param_1 = obj.book(start,end);
*/```
```using System;

public class MyCalendar
{
private SortedList calendar;

public MyCalendar()
{
calendar = new SortedList();
}

public bool Book(int start, int end)
{
int left = 0, right = calendar.Count - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (calendar[mid].Key >= end)
{
right = mid - 1;
}
else if (calendar[mid].Value <= start)
{
left = mid + 1;
}
else
{
return false;
}
}