-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMy-calender-1.java
286 lines (222 loc) · 9.08 KB
/
My-calender-1.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
class MyCalendar {
class Node {
int start;
int end;
Node left,right;
public Node(int start,int end){
this.start = start;
this.end = end;
}
}
Node root;
public MyCalendar() {
}
public boolean book(int start, int end) {
if(root == null){
root = new Node(start,end);
return true;
}
Node curr = root;
while(curr != null){
if(end <= curr.start){
if(curr.left == null){
curr.left = new Node(start,end);
return true;
}
curr = curr.left;
}
else if(start >= curr.end){
if(curr.right == null){
curr.right = new Node(start,end);
return true;
}
curr = curr.right;
}
else return false;
}
return false;
}
}
/**
This implementation of `MyCalendar` uses a binary search tree (BST) to efficiently manage the booking of events without overlapping. Each node in the tree represents an event, with `start` and `end` representing the start and end times of that event.
### Explanation:
#### 1. **Node Class**:
```java
class Node {
int start;
int end;
Node left, right;
public Node(int start, int end) {
this.start = start;
this.end = end;
}
}
```
- Each node stores the `start` and `end` times of an event.
- `left` points to events that end before this event starts.
- `right` points to events that start after this event ends.
#### 2. **Root Initialization**:
```java
Node root;
```
- The tree has a `root` node, which is initialized as `null` in the constructor.
```java
public MyCalendar() {}
```
- The constructor initializes the `root` of the binary tree as `null` because initially, there are no booked events.
#### 3. **book Method**:
The `book` method attempts to add a new event `[start, end)` to the calendar while ensuring there is no overlap with any existing events.
```java
public boolean book(int start, int end) {
if (root == null) {
root = new Node(start, end);
return true;
}
```
- If the tree is empty (`root == null`), the event is added as the root node, and the method returns `true`.
#### 4. **Traversing the Tree**:
The method then traverses the tree to find the correct position for the new event, ensuring no overlap with any existing event.
```java
Node curr = root;
while (curr != null) {
```
- The method starts at the root and iteratively traverses down the tree.
- **If the new event ends before the current event starts**:
```java
if (end <= curr.start) {
if (curr.left == null) {
curr.left = new Node(start, end);
return true;
}
curr = curr.left;
}
```
- This means the new event should be placed in the left subtree (since there is no overlap).
- If the left child is `null`, the new event is inserted as the left child.
- If not, the traversal continues to the left child.
- **If the new event starts after the current event ends**:
```java
else if (start >= curr.end) {
if (curr.right == null) {
curr.right = new Node(start, end);
return true;
}
curr = curr.right;
}
```
- This means the new event should be placed in the right subtree (since there is no overlap).
- If the right child is `null`, the new event is inserted as the right child.
- If not, the traversal continues to the right child.
- **If the new event overlaps with the current event**:
```java
else return false;
```
- If neither condition holds (i.e., `end > curr.start` and `start < curr.end`), the new event overlaps with an existing one, so the booking fails, and `false` is returned.
#### 5. **Edge Cases**:
- If the new event is disjoint from all existing events, it will be inserted in the appropriate position in the BST, either as a left or right child.
- The tree is traversed using a `while` loop, ensuring that the insertion or conflict detection happens efficiently.
### Summary:
- This solution uses a binary search tree (`BST`) to store events, where each node represents an event with a `start` and `end` time.
- The `book` method tries to insert a new event, ensuring no overlap by checking the relative positions of the new event against existing ones.
- Time complexity for insertion and search is on average `O(log n)` for balanced trees but can degrade to `O(n)` in the worst case for unbalanced trees. */
//option2
class MyCalendar {
TreeMap<Integer, Integer> calendar;
MyCalendar() {
calendar = new TreeMap();
}
public boolean book(int start, int end) {
Integer prev = calendar.floorKey(start),
next = calendar.ceilingKey(start);
if ((prev == null || calendar.get(prev) <= start) &&
(next == null || end <= next)) {
calendar.put(start, end);
return true;
}
return false;
}
}
/**
The `MyCalendar` class implements a booking system that allows users to book events, ensuring that no two events overlap. It uses a `TreeMap` to store and manage the events, where the keys represent the start times and the values represent the end times of the booked events.
Here’s a detailed breakdown of how the class works:
### 1. **TreeMap Initialization**:
```java
TreeMap<Integer, Integer> calendar;
```
- `TreeMap` is a data structure that stores key-value pairs and automatically keeps the keys in sorted order.
- In this case, the keys are event start times, and the values are the corresponding end times.
```java
calendar = new TreeMap();
```
- The constructor initializes the `TreeMap` to manage the events.
### 2. **`book` Method**:
This method checks if an event can be booked without overlapping any existing events.
```java
public boolean book(int start, int end)
```
- **Parameters**:
- `start`: The start time of the new event.
- `end`: The end time of the new event (exclusive, meaning the event ends right before this time).
- **Logic**:
- The method checks if there is any overlap with the previously booked event (`prev`) or the next booked event (`next`).
```java
Integer prev = calendar.floorKey(start),
next = calendar.ceilingKey(start);
```
- `prev`: The `floorKey` returns the largest key (start time) that is less than or equal to `start`. This represents the previous event's start time.
- `next`: The `ceilingKey` returns the smallest key (start time) that is greater than or equal to `start`. This represents the next event's start time.
Now, the method checks two conditions:
```java
if ((prev == null || calendar.get(prev) <= start) && (next == null || end <= next))
```
- **Condition 1**: `(prev == null || calendar.get(prev) <= start)`
- This checks whether there is no previous event (`prev == null`) or the previous event ends before or at the new event’s start time (`calendar.get(prev) <= start`).
- **Condition 2**: `(next == null || end <= next)`
- This checks whether there is no next event (`next == null`) or the new event ends before the next event starts (`end <= next`).
If both conditions are satisfied (i.e., no overlap with either previous or next events), the event is added to the `calendar`:
```java
calendar.put(start, end);
return true;
```
If there is an overlap, the method returns `false`.
### 3. **Example**:
```java
MyCalendar obj = new MyCalendar();
boolean result = obj.book(10, 20); // This will return true if the event [10, 20) can be booked.
```
### Summary:
- The `TreeMap` structure ensures that events are stored in a sorted manner by start time.
- The `book` method checks if the new event overlaps with the nearest previous and next events using `floorKey` and `ceilingKey`.
- If there is no overlap, the event is booked (added to the `TreeMap`), and the method returns `true`. If there is an overlap, it returns `false`.
*/
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/
// option3 -BRUTE FORCE
class MyCalendar {
List<int[]> calender;
public MyCalendar() {
calender=new ArrayList<>();
}
public boolean book(int start, int end) {
for(int[] slot:calender){
if(end>slot[0] && start<slot[1]){
return false;
}
}
calender.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);
*/
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/