Skip to content

Commit b7e2366

Browse files
authored
Create my-calendar-i.py
1 parent aa2d2d0 commit b7e2366

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Python/my-calendar-i.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Time: O(n^2)
2+
# Space: O(n)
3+
4+
# Implement a MyCalendar class to store your events.
5+
# A new event can be added if adding the event will not cause a double booking.
6+
#
7+
# Your class will have the method, book(int start, int end).
8+
# Formally, this represents a booking on the half open interval [start, end),
9+
# the range of real numbers x such that start <= x < end.
10+
#
11+
# A double booking happens when two events have some non-empty intersection
12+
# (ie., there is some time that is common to both events.)
13+
#
14+
# For each call to the method MyCalendar.book,
15+
# return true if the event can be added to the calendar successfully without causing a double booking.
16+
# Otherwise, return false and do not add the event to the calendar.
17+
#
18+
# Your class will be called like this: MyCalendar cal = new MyCalendar(); MyCalendar.book(start, end)
19+
# Example 1:
20+
# MyCalendar();
21+
# MyCalendar.book(10, 20); // returns true
22+
# MyCalendar.book(15, 25); // returns false
23+
# MyCalendar.book(20, 30); // returns true
24+
# Explanation:
25+
# The first event can be booked. The second can't because time 15 is already booked by another event.
26+
# The third event can be booked, as the first event takes every time less than 20, but not including 20.
27+
#
28+
# Note:
29+
# - The number of calls to MyCalendar.book per test case will be at most 1000.
30+
# - In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9].
31+
32+
class MyCalendar(object):
33+
34+
def __init__(self):
35+
self.__calendar = []
36+
37+
38+
def book(self, start, end):
39+
"""
40+
:type start: int
41+
:type end: int
42+
:rtype: bool
43+
"""
44+
for i, j in self.__calendar:
45+
if start < j and end > i:
46+
return False
47+
self.__calendar.append((start, end))
48+
return True
49+
50+
51+
# Your MyCalendar object will be instantiated and called as such:
52+
# obj = MyCalendar()
53+
# param_1 = obj.book(start,end)

0 commit comments

Comments
 (0)