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 20, 2024
1 parent 5ffe63c commit 500b3da
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 9 deletions.
54 changes: 54 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Array/MyCalendar1.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,60 @@ public boolean book(int start, int end) {
}
}

// V0-1
// IDEA : ARRAY + BOUNDARY HANDLING
class MyCalendar_0_1 {

List<List<Integer>> bookings;

public MyCalendar_0_1() {
this.bookings = new ArrayList<>();
}

public boolean book(int start, int end) {
if (this.bookings.size()==0){
List<Integer> newBooking = new ArrayList<>();
newBooking.add(start);
newBooking.add(end);
this.bookings.add(newBooking);
return true;
}
/**
* Overlap 3 cases
*
* case 1)
*
* |-----| old new[1] > old[0] && new[0] < old[1]
* |----| new
*
*
* case 2)
* |-----| old new[1] > old[0] && new[0] < old[1]
* |-----| new
*
* case 3)
*
* |------| old new[1] > old[0] && new[0] < old[1]
* |-------------| new
*
*
* -> so, all overlap cases
* -> are with condition : "new[1] > old[0] && new[0] < old[1]"
*/
for (List<Integer> booking: bookings){
// NOTE !!! check if overlap happens
if (booking.get(1) > start && booking.get(0) < end){
return false;
}
}
List<Integer> newBooking = new ArrayList<>();
newBooking.add(start);
newBooking.add(end);
this.bookings.add(newBooking);
return true;
}
}

// V1-1
// IDEA : BRUTE FORCE
// https://leetcode.com/problems/my-calendar-i/editorial/
Expand Down
74 changes: 65 additions & 9 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -1707,26 +1707,82 @@ private int dfsCount(TreeNode root){
* 20 30 -> true
*
*/
class MyCalendar {
List<List<Integer>> dates;
// https://leetcode.com/problems/my-calendar-i/
// 4.37 pm - 5.00 pm
class MyCalendar {

List<List<Integer>> bookings;

public MyCalendar() {
this.dates = new ArrayList<>();
this.bookings = new ArrayList<>();
}

public boolean book(int start, int end) {
for (List<Integer> date : dates){
if (start < date.get(1) && end < date.get(0)){
if (this.bookings.size()==0){
List<Integer> newBooking = new ArrayList<>();
newBooking.add(start);
newBooking.add(end);
this.bookings.add(newBooking);
return true;
}
/**
* overlap 3 cases
*
* case 1)
*
* |-----| old new[1] > old[0] && new[0] < old[1]
* |----| new
*
*
* case 2)
* |-----| old new[1] > old[0] && new[0] < old[1]
* |-----| new
*
* case 3)
*
* |------| old new[1] > old[0] && new[0] < old[1]
* |-------------| new
*
*/
for (List<Integer> booking: bookings){
if (booking.get(1) > start && booking.get(0) < end){
return false;
}
}
List<Integer> newBook = new ArrayList<>();
newBook.add(start);
newBook.add(end);
this.dates.add(newBook);
List<Integer> newBooking = new ArrayList<>();
newBooking.add(start);
newBooking.add(end);
this.bookings.add(newBooking);
return true;
}
}

/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/

// class MyCalendar {
// List<List<Integer>> dates;
// public MyCalendar() {
// this.dates = new ArrayList<>();
// }
//
// public boolean book(int start, int end) {
// for (List<Integer> date : dates){
// if (start < date.get(1) && end < date.get(0)){
// return false;
// }
// }
// List<Integer> newBook = new ArrayList<>();
// newBook.add(start);
// newBook.add(end);
// this.dates.add(newBook);
// return true;
// }
// }


// class MyCalendar {
// List<List<Integer>> dates;
Expand Down

0 comments on commit 500b3da

Please sign in to comment.