731. My Calendar II
Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event will not cause a triple booking.
Your class will have one method, book(int start, int end), where start and end represent the start and end times of the event. The method should return true if the event can be added without causing a triple booking, and false otherwise.
An event is a triple booking if there are three events that overlap at some point.
MyCalendarTwo myCalendar = new MyCalendarTwo();
myCalendar.book(10, 20); // returns true
myCalendar.book(50, 60); // returns true
myCalendar.book(10, 40); // returns true
myCalendar.book(5, 15); // returns false
myCalendar.book(5, 10); // returns true
myCalendar.book(25, 55); // returns true
start and end just before end.start and end?
start < end.start and end values be negative or zero?
start, end ≤ 10^9).We can keep track of two calendars:
When trying to book a new event, we first check if it will cause a triple booking by overlapping with any of the double bookings. If it does, we return false. If not, we then update our single and double booking tracks accordingly and return true.
class MyCalendarTwo:
def __init__(self):
self.bookings = []
self.overlaps = []
def book(self, start: int, end: int) -> bool:
for o_start, o_end in self.overlaps:
if start < o_end and end > o_start:
return False
for b_start, b_end in self.bookings:
if start < b_end and end > b_start:
self.overlaps.append((max(start, b_start), min(end, b_end)))
self.bookings.append((start, end))
return True
# Example usage:
# myCalendar = MyCalendarTwo()
# print(myCalendar.book(10, 20)) # returns True
# print(myCalendar.book(50, 60)) # returns True
# print(myCalendar.book(10, 40)) # returns True
# print(myCalendar.book(5, 15)) # returns False
# print(myCalendar.book(5, 10)) # returns True
# print(myCalendar.book(25, 55)) # returns True
Let ( n ) be the number of events booked so far.
__init__) runs in O(1) time.Overall, the implementation ensures that we iteratively check and record overlaps, maintaining efficiency within acceptable limits for typical input sizes expected in such problems.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?