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.
思路:标准的扫描线,每次需要排序;但是performance,比treemap要快;
class MyCalendarTwo {
private class Node {
public int index;
public int flag;
public Node(int index, int flag) {
this.index = index;
this.flag = flag;
}
}
private List<Node> list;
public MyCalendarTwo() {
list = new ArrayList<Node>();
}
public boolean book(int start, int end) {
Node startNode = new Node(start, 1);
Node endNode = new Node(end, -1);
list.add(startNode);
list.add(endNode);
Collections.sort(list, (a, b) -> (a.index != b.index ? a.index - b.index : a.flag - b.flag));
int count = 0;
int maxcount = 0;
for(Node node: list) {
if(node.flag == 1) {
count++;
} else {
count--;
}
maxcount = Math.max(maxcount, count);
}
if(maxcount >= 3) {
list.remove(startNode);
list.remove(endNode);
}
return maxcount <= 2 ? true : false;
}
}
/**
* Your MyCalendarTwo object will be instantiated and called as such:
* MyCalendarTwo obj = new MyCalendarTwo();
* boolean param_1 = obj.book(start,end);
*/
思路:扫描线,用treemap<Integer, Integer> ( key x坐标,value: 起飞降落的个数)来代替priorityqueue做,start + 1, end -1, Treemap,自动维护x坐标的从小到大。T: 加入是 lgn, 扫描是O(N);
class MyCalendarTwo {
TreeMap<Integer, Integer> treeMap;
public MyCalendarTwo() {
treeMap = new TreeMap<Integer, Integer>();
}
public boolean book(int start, int end) {
// add it;
treeMap.put(start, treeMap.getOrDefault(start, 0) + 1);
treeMap.put(end, treeMap.getOrDefault(end, 0) - 1);
// check if sweep line count > 3 or not;
int count = 0;
for(Integer key: treeMap.keySet()) {
count += treeMap.get(key);
if(count >= 3) {
treeMap.put(start, treeMap.getOrDefault(start, 0) - 1);
treeMap.put(end, treeMap.getOrDefault(end, 0) + 1);
return false;
}
}
return true;
}
}
/**
* Your MyCalendarTwo object will be instantiated and called as such:
* MyCalendarTwo obj = new MyCalendarTwo();
* boolean param_1 = obj.book(start,end);
*/