-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path729. My Calendar I.java
56 lines (56 loc) · 1.53 KB
/
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
class Pair extends LinkedList<Pair> {
int start;
int end;
Pair next;
Pair(int start, int end) {
this.start = start;
this.end = end;
}
}
class MyCalendar {
private Pair eventStorage;
public MyCalendar() {
Pair dummyTail = new Pair(Integer.MAX_VALUE, Integer.MAX_VALUE);
this.eventStorage = new Pair(-1, -1);
this.eventStorage.next = (dummyTail);
}
public boolean book(int start, int end) {
Pair currentNode = this.eventStorage;
Pair tempNode = this.eventStorage;
/*
* ami joto khon na faka starttime pabo curr node k egiye niye jabo
*/
while (currentNode.start < start) {
tempNode = currentNode;
currentNode = currentNode.next;
}
// jodi already sesh besi event reggister kora thake
// OR: prebooked kono event already ache, jar start time end er interval e
// conflict korche
if (tempNode.end > start || currentNode.start < end) {
return false;
}
Pair newEvent = new Pair(start, end);
newEvent.next = currentNode;
tempNode.next = newEvent;
// means booking newa hoye gelo
return true;
}
}
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/