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). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.
A triple booking happens when three events have some non-empty intersection (ie., there is some time that is common to all 3 events.)
For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.
Your class will be called like this: MyCalendar cal = new MyCalendar();MyCalendar.book(start, end)
Example 1:
MyCalendar(); 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 Explanation: The first two events can be booked. The third event can be double booked. The fourth event (5, 15) can't be booked, because it would result in a triple booking. The fifth event (5, 10) can be booked, as it does not use time 10 which is already double booked. The sixth event (25, 55) can be booked, as the time in [25, 40) will be double booked with the third event; the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.Note:
The number of calls to MyCalendar.book per test case will be at most 1000.In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9].
分析
这道题与My Calendar I 相似,但是这道题可以允许有两次重复,不允许有第三次重复。之前的想法是对所有的区间进行归纳和重新分配,并添加计数,但是这样的话写起来很难,因为涉及到多个区间的拆分,一时半会我也写不出来。
简单一点的办法可以用另外一个conflict数组记录当前所有重复的时间的区间,当新加入的时间段与conflict中的时间段有重复时,就直接返回false。如果没有重复,那么就遍历当前calendar的所有时间段,将重复的时间段加入到conflict中。
判断时间段是否重复的方法:[s1, e1)与[s2, e2],s = max(s1, s2), e = min(e1, e2),如果s < e ,则说明时间段重复,重复的时间段为[s, e)。
Code
class MyCalendarTwo { public: MyCalendarTwo() { cal.clear(); } bool book(int start, int end) { int index = findIndex(start, 0, cal.size() -1); bool ret = getConflict(start, end, index); if (ret == false) return false; vector<int> tmp; tmp.push_back(start); tmp.push_back(end); cal.insert(cal.begin() + index, tmp); return true; } bool getConflict(int start, int end, int index) { for (int i = 0; i < conflict.size(); i ++) { int s = max(conflict[i][0], start); int e = min(conflict[i][1], end); if (s < e) return false; } for (int i = 0; i < cal.size(); i ++) { vector<int> tmp; tmp.push_back(max(cal[i][0], start)); tmp.push_back(min(cal[i][1], end)); if (tmp[0] >= tmp[1]) continue; conflict.push_back(tmp); } return true; } int findIndex(int start, int left, int right) { if (left > right) return left; int middle = (left + right)/2; if (cal[middle][0] == start) return middle; if (cal[middle][0] > start) return findIndex(start, left, middle - 1); else return findIndex(start, middle+1, right); } vector<vector<int>> cal; vector<vector<int>> conflict; }; /** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo* obj = new MyCalendarTwo(); * bool param_1 = obj->book(start,end); */运行效率
Runtime: 1648 ms, faster than 5.00% of C++ online submissions for My Calendar II.
Memory Usage: 343.7 MB, less than 5.10% of C++ online submissions for My Calendar II.