#729
My Calendar I
specialist · 735 · lc medium +31 · 58.2% accepted · 4,812 likes · top 54%
Description
Build a calendar that rejects events that would cause a double-booking — a situation where two scheduled events share any overlapping time. Events are represented as half-open intervals [startTime, endTime).
Implement the MyCalendar class:
- MyCalendar() Creates an empty calendar.
- boolean book(int startTime, int endTime) Attempts to add the event. Returns true and records the event if no overlap exists with any existing booking; otherwise returns false and leaves the calendar unchanged.
Example 1:
Input
["MyCalendar", "book", "book", "book"]
[[], [10, 20], [15, 25], [20, 30]]
Output
[null, true, false, true]
Example 2:
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.
Code
1
2
3
4
5
6
7
8
9
10
11
12