729. My Calendar I LeetCode Solution
In this guide, you will get 729. My Calendar I LeetCode Solution with the best time and space complexity. The solution to My Calendar I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- My Calendar I solution in C++
- My Calendar I solution in Java
- My Calendar I solution in Python
- Additional Resources
Problem Statement of My Calendar I
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.
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(1)
729. My Calendar I LeetCode Solution in C++
class MyCalendar {
public:
bool book(int start, int end) {
for (const auto& [s, e] : timeline)
if (max(start, s) < min(end, e))
return false;
timeline.emplace_back(start, end);
return true;
}
private:
vector<pair<int, int>> timeline;
};
/* code provided by PROGIEZ */
729. My Calendar I LeetCode Solution in Java
class MyCalendar {
public boolean book(int start, int end) {
for (int[] t : timeline)
if (Math.max(t[0], start) < Math.min(t[1], end))
return false;
timeline.add(new int[] {start, end});
return true;
}
private List<int[]> timeline = new ArrayList<>();
}
// code provided by PROGIEZ
729. My Calendar I LeetCode Solution in Python
class MyCalendar:
def __init__(self):
self.timeline = []
def book(self, start: int, end: int) -> bool:
for s, e in self.timeline:
if max(start, s) < min(end, e):
return False
self.timeline.append((start, end))
return True
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.