-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path729.java
More file actions
72 lines (64 loc) · 2.03 KB
/
729.java
File metadata and controls
72 lines (64 loc) · 2.03 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
__________________________________________________________________________________________________
sample 51 ms submission
class MyCalendar {
public MyCalendar() {
}
private Node root;
public boolean book(int start, int end) {
if(root==null) {
root=new Node(start,end);
return true;
}
return add(root,start,end,Integer.MIN_VALUE,Integer.MAX_VALUE);
}
private boolean add(Node node, int start, int end, int min, int max) { //end can be max, start can be min
int v_s=node.start;
int v_e=node.end;
if(start<min ||end<min || start>max|| end>max) {return false;}
if(end<=v_s) {
if(node.left==null) {
node.left=new Node(start,end);
return true;
} else {
return add(node.left,start,end,min,v_s);
}
} else if(start>=v_e) {
if(node.right==null) {
node.right=new Node(start,end);
return true;
} else {
return add(node.right,start,end,v_e,max);
}
} else {
return false;
}
}
class Node {
int start;
int end;
Node left;
Node right;
Node(int s, int e) {
start=s;
end=e;
}
}
}
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/
__________________________________________________________________________________________________
sample 36232 kb submission
class MyCalendar {
List<int[]> booking = new ArrayList<>();
public boolean book(int start, int end) {
for (int[] b : booking) {
if (Math.max(b[0], start) < Math.min(b[1], end)) return false;
}
booking.add(new int[]{start, end});
return true;
}
}
__________________________________________________________________________________________________