Skip to content

Commit 500b3da

Browse files
committed
update 729 java
1 parent 5ffe63c commit 500b3da

File tree

2 files changed

+119
-9
lines changed

2 files changed

+119
-9
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,60 @@ public boolean book(int start, int end) {
8383
}
8484
}
8585

86+
// V0-1
87+
// IDEA : ARRAY + BOUNDARY HANDLING
88+
class MyCalendar_0_1 {
89+
90+
List<List<Integer>> bookings;
91+
92+
public MyCalendar_0_1() {
93+
this.bookings = new ArrayList<>();
94+
}
95+
96+
public boolean book(int start, int end) {
97+
if (this.bookings.size()==0){
98+
List<Integer> newBooking = new ArrayList<>();
99+
newBooking.add(start);
100+
newBooking.add(end);
101+
this.bookings.add(newBooking);
102+
return true;
103+
}
104+
/**
105+
* Overlap 3 cases
106+
*
107+
* case 1)
108+
*
109+
* |-----| old new[1] > old[0] && new[0] < old[1]
110+
* |----| new
111+
*
112+
*
113+
* case 2)
114+
* |-----| old new[1] > old[0] && new[0] < old[1]
115+
* |-----| new
116+
*
117+
* case 3)
118+
*
119+
* |------| old new[1] > old[0] && new[0] < old[1]
120+
* |-------------| new
121+
*
122+
*
123+
* -> so, all overlap cases
124+
* -> are with condition : "new[1] > old[0] && new[0] < old[1]"
125+
*/
126+
for (List<Integer> booking: bookings){
127+
// NOTE !!! check if overlap happens
128+
if (booking.get(1) > start && booking.get(0) < end){
129+
return false;
130+
}
131+
}
132+
List<Integer> newBooking = new ArrayList<>();
133+
newBooking.add(start);
134+
newBooking.add(end);
135+
this.bookings.add(newBooking);
136+
return true;
137+
}
138+
}
139+
86140
// V1-1
87141
// IDEA : BRUTE FORCE
88142
// https://leetcode.com/problems/my-calendar-i/editorial/

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,26 +1707,82 @@ private int dfsCount(TreeNode root){
17071707
* 20 30 -> true
17081708
*
17091709
*/
1710-
class MyCalendar {
1711-
List<List<Integer>> dates;
1710+
// https://leetcode.com/problems/my-calendar-i/
1711+
// 4.37 pm - 5.00 pm
1712+
class MyCalendar {
1713+
1714+
List<List<Integer>> bookings;
1715+
17121716
public MyCalendar() {
1713-
this.dates = new ArrayList<>();
1717+
this.bookings = new ArrayList<>();
17141718
}
17151719

17161720
public boolean book(int start, int end) {
1717-
for (List<Integer> date : dates){
1718-
if (start < date.get(1) && end < date.get(0)){
1721+
if (this.bookings.size()==0){
1722+
List<Integer> newBooking = new ArrayList<>();
1723+
newBooking.add(start);
1724+
newBooking.add(end);
1725+
this.bookings.add(newBooking);
1726+
return true;
1727+
}
1728+
/**
1729+
* overlap 3 cases
1730+
*
1731+
* case 1)
1732+
*
1733+
* |-----| old new[1] > old[0] && new[0] < old[1]
1734+
* |----| new
1735+
*
1736+
*
1737+
* case 2)
1738+
* |-----| old new[1] > old[0] && new[0] < old[1]
1739+
* |-----| new
1740+
*
1741+
* case 3)
1742+
*
1743+
* |------| old new[1] > old[0] && new[0] < old[1]
1744+
* |-------------| new
1745+
*
1746+
*/
1747+
for (List<Integer> booking: bookings){
1748+
if (booking.get(1) > start && booking.get(0) < end){
17191749
return false;
17201750
}
17211751
}
1722-
List<Integer> newBook = new ArrayList<>();
1723-
newBook.add(start);
1724-
newBook.add(end);
1725-
this.dates.add(newBook);
1752+
List<Integer> newBooking = new ArrayList<>();
1753+
newBooking.add(start);
1754+
newBooking.add(end);
1755+
this.bookings.add(newBooking);
17261756
return true;
17271757
}
17281758
}
17291759

1760+
/**
1761+
* Your MyCalendar object will be instantiated and called as such:
1762+
* MyCalendar obj = new MyCalendar();
1763+
* boolean param_1 = obj.book(start,end);
1764+
*/
1765+
1766+
// class MyCalendar {
1767+
// List<List<Integer>> dates;
1768+
// public MyCalendar() {
1769+
// this.dates = new ArrayList<>();
1770+
// }
1771+
//
1772+
// public boolean book(int start, int end) {
1773+
// for (List<Integer> date : dates){
1774+
// if (start < date.get(1) && end < date.get(0)){
1775+
// return false;
1776+
// }
1777+
// }
1778+
// List<Integer> newBook = new ArrayList<>();
1779+
// newBook.add(start);
1780+
// newBook.add(end);
1781+
// this.dates.add(newBook);
1782+
// return true;
1783+
// }
1784+
// }
1785+
17301786

17311787
// class MyCalendar {
17321788
// List<List<Integer>> dates;

0 commit comments

Comments
 (0)