Time = O(n^2), Space = O(n)
class MyCalendar {
List<int[]> times;
public MyCalendar() {
times = new ArrayList<>();
}
public boolean book(int start, int end) {
for (int[] time : times) {
if (Math.max(start, time[0]) < Math.min(end, time[1])) {
return false;
}
}
times.add(new int[]{start, end});
return true;
}
}
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/
Time = O(nlogn), Space = O(n)
Referred to the post here.
class MyCalendar {
TreeMap<Integer, Integer> calendar;
public MyCalendar() {
calendar = new TreeMap<>();
}
public boolean book(int start, int end) {
Integer floorKey = calendar.floorKey(start);
if (floorKey != null && calendar.get(floorKey) > start) {
return false;
}
Integer ceilingKey = calendar.ceilingKey(start);
if (ceilingKey != null && ceilingKey < end) {
return false;
}
calendar.put(start, end);
return true;
}
}
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/