Skip to content

Commit 956d4e1

Browse files
committed
update 729 java
1 parent 008a5d6 commit 956d4e1

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

doc/cheatsheet/array_overlap_explaination.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
# Explanation of Overlapping Booking Logic in MyCalendar
33

4+
- (LC 729)
5+
46
This document explains the logic used in the following code snippet from the `MyCalendar` class:
57

68
```java

leetcode_java/src/main/java/LeetCodeJava/Array/MyCalendar1.java

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,94 @@ public boolean book(int start, int end) {
131131
}
132132
}
133133

134-
// V2
134+
// V2-1
135+
// IDEA : LINKEDLIST
136+
// https://leetcode.com/problems/my-calendar-i/solutions/1262570/js-python-java-c-easy-sorted-tree-linked-list-solutions-w-explanation/
137+
class ListNode {
138+
public int start, end;
139+
public ListNode next;
140+
141+
public ListNode(int s, int e, ListNode n) {
142+
start = s;
143+
end = e;
144+
next = n;
145+
}
146+
}
147+
148+
class MyCalendar_2_1 {
149+
ListNode calendar;
150+
151+
public MyCalendar_2_1() {
152+
ListNode tail = new ListNode(Integer.MAX_VALUE, Integer.MAX_VALUE, null);
153+
calendar = new ListNode(-1, -1, tail);
154+
}
155+
156+
public boolean book(int start, int end) {
157+
ListNode curr = calendar, last = curr;
158+
while (start >= curr.end) {
159+
last = curr;
160+
curr = curr.next;
161+
}
162+
if (curr.start < end)
163+
return false;
164+
last.next = new ListNode(start, end, curr);
165+
return true;
166+
}
167+
}
168+
169+
// V3
170+
// IDEA : LINKEDLIST
171+
// https://leetcode.com/problems/my-calendar-i/solutions/2372060/java-easy-solution-100-faster-code/
172+
class Node {
173+
int start, end;
174+
Node left;
175+
Node right;
176+
177+
public Node(int start, int end) {
178+
this.start = start;
179+
this.end = end;
180+
left = null;
181+
right = null;
182+
}
183+
}
184+
185+
class MyCalendar_3 {
186+
187+
Node root;
188+
189+
public MyCalendar_3() {
190+
this.root = null;
191+
192+
}
193+
194+
public boolean insert(Node parent, int s, int e) {
195+
if (parent.start >= e) {
196+
if (parent.left == null) {
197+
parent.left = new Node(s, e);
198+
return true;
199+
} else {
200+
return insert(parent.left, s, e);
201+
}
202+
} else if (parent.end <= s) {
203+
if (parent.right == null) {
204+
parent.right = new Node(s, e);
205+
return true;
206+
} else {
207+
return insert(parent.right, s, e);
208+
}
209+
}
210+
211+
return false;
212+
}
213+
214+
public boolean book(int start, int end) {
215+
if (root == null) {
216+
root = new Node(start, end);
217+
return true;
218+
} else {
219+
return insert(root, start, end);
220+
}
221+
}
222+
}
223+
135224
}

0 commit comments

Comments
 (0)