729. My Calendar I
https://leetcode.com/problems/my-calendar-i/
注意这道题只需要检查最接近的两个时间就可以了(floorKey + ceilingKey),它只能保存不重叠的event所以不会出现一个事情开始得早结束得晚的,因为这样会跟最接近的两个重叠。
class MyCalendar {
private TreeMap<Integer, Integer> map = new TreeMap<>();
public MyCalendar() {
}
public boolean book(int start, int end) {
Integer prev = map.floorKey(start);
Integer next = map.ceilingKey(start);
if ((prev == null || map.get(prev) <= start) && (next == null || next >= end)) {
map.put(start, end);
return true;
}
return false;
}
}
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/
Previous1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to LimitNext218 The Skyline Problem
Last updated