-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCalendar.java
More file actions
38 lines (29 loc) · 896 Bytes
/
MyCalendar.java
File metadata and controls
38 lines (29 loc) · 896 Bytes
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
import java.util.Map;
import java.util.TreeMap;
/**
* @author Lillard
*/
class MyCalendar {
private TreeMap<Integer, Integer> bookMap;
public MyCalendar() {
bookMap = new TreeMap<>();
}
public boolean book(int start, int end) {
Map.Entry leftRange = bookMap.lowerEntry(start);
if (leftRange != null && (int)leftRange.getValue() > start) {
return false;
}
Integer rightIndex = bookMap.higherKey(start - 1);
if (rightIndex != null && rightIndex < end) {
return false;
}
bookMap.put(start, end);
return true;
}
public static void main(String[] args) {
MyCalendar myCalendar = new MyCalendar();
System.out.println(myCalendar.book(10, 20));
System.out.println(myCalendar.book(15, 25));
System.out.println(myCalendar.book(20, 30));
}
}