-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-729-My-Calendar-I.java
60 lines (50 loc) · 1.51 KB
/
LeetCode-729-My-Calendar-I.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 1. TreeMap
// class MyCalendar {
// TreeMap<Integer, Integer> map;
// public MyCalendar() {
// map = new TreeMap<>();
// }
// public boolean book(int start, int end) {
// Map.Entry floorEntry = map.floorEntry(start);
// Map.Entry ceilingEntry = map.ceilingEntry(start);
// if (floorEntry != null && (int) floorEntry.getValue() > start) return false;
// if (ceilingEntry != null && (int) ceilingEntry.getKey() < end) return false;
// map.put(start, end);
// return true;
// }
// }
// 2. Binary Search
/*
Search the insert index in the sorted array.
*/
class MyCalendar {
List<int[]> bookings;
public MyCalendar() {
bookings=new LinkedList<>();
}
public boolean book(int start, int end) {
int s=0, e=bookings.size()-1;
while (s<=e){
int mid=s+(e-s)/2;
int[] bk=bookings.get(mid);
if (bk[0]==start){
return false;
}else if (bk[0]>start){
e=mid-1;
}else{
s=mid+1;
}
}
if (s>0 && start<bookings.get(s-1)[1])
return false;
if (s<bookings.size() && end>bookings.get(s)[0])
return false;
bookings.add(s, 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);
*/