Skip to content

Commit

Permalink
update 729 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 19, 2024
1 parent 008a5d6 commit 956d4e1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/cheatsheet/array_overlap_explaination.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

# Explanation of Overlapping Booking Logic in MyCalendar

- (LC 729)

This document explains the logic used in the following code snippet from the `MyCalendar` class:

```java
Expand Down
91 changes: 90 additions & 1 deletion leetcode_java/src/main/java/LeetCodeJava/Array/MyCalendar1.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,94 @@ public boolean book(int start, int end) {
}
}

// V2
// V2-1
// IDEA : LINKEDLIST
// https://leetcode.com/problems/my-calendar-i/solutions/1262570/js-python-java-c-easy-sorted-tree-linked-list-solutions-w-explanation/
class ListNode {
public int start, end;
public ListNode next;

public ListNode(int s, int e, ListNode n) {
start = s;
end = e;
next = n;
}
}

class MyCalendar_2_1 {
ListNode calendar;

public MyCalendar_2_1() {
ListNode tail = new ListNode(Integer.MAX_VALUE, Integer.MAX_VALUE, null);
calendar = new ListNode(-1, -1, tail);
}

public boolean book(int start, int end) {
ListNode curr = calendar, last = curr;
while (start >= curr.end) {
last = curr;
curr = curr.next;
}
if (curr.start < end)
return false;
last.next = new ListNode(start, end, curr);
return true;
}
}

// V3
// IDEA : LINKEDLIST
// https://leetcode.com/problems/my-calendar-i/solutions/2372060/java-easy-solution-100-faster-code/
class Node {
int start, end;
Node left;
Node right;

public Node(int start, int end) {
this.start = start;
this.end = end;
left = null;
right = null;
}
}

class MyCalendar_3 {

Node root;

public MyCalendar_3() {
this.root = null;

}

public boolean insert(Node parent, int s, int e) {
if (parent.start >= e) {
if (parent.left == null) {
parent.left = new Node(s, e);
return true;
} else {
return insert(parent.left, s, e);
}
} else if (parent.end <= s) {
if (parent.right == null) {
parent.right = new Node(s, e);
return true;
} else {
return insert(parent.right, s, e);
}
}

return false;
}

public boolean book(int start, int end) {
if (root == null) {
root = new Node(start, end);
return true;
} else {
return insert(root, start, end);
}
}
}

}

0 comments on commit 956d4e1

Please sign in to comment.