Skip to content

Commit cc32d53

Browse files
committed
add 729 java, array_overlap_explaination.md
1 parent a036a9a commit cc32d53

File tree

6 files changed

+332
-15
lines changed

6 files changed

+332
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
717 | [1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/) | [Python](./leetcode_python/Array/1-bit-and-2-bit-characters.py) | _O(n)_ | _O(1)_ | Easy | Greedy | AGAIN (not start)
270270
723 | [Candy Crush](https://leetcode.com/problems/candy-crush/) | [Python](./leetcode_python/Array/candy-crush.py) | _O((R * C)^2)_ | _O(1)_ | Medium |`complex`| AGAIN (not start)
271271
724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Python](./leetcode_python/Array/find-pivot-index.py) | _O(n)_ | _O(1)_ | Easy || OK*
272-
729 | [My Calendar I](https://leetcode.com/problems/my-calendar-i/) | [Python](./leetcode_python/Array/my-calendar-i.py) | _O(nlogn)_ | _O(n)_ | Medium |`python class`| AGAIN* (not start)
272+
729 | [My Calendar I](https://leetcode.com/problems/my-calendar-i/) | [Python](./leetcode_python/Array/my-calendar-i.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MyCalendar1.java) | _O(nlogn)_ | _O(n)_ | Medium |good basic, brute force, binary search, google| AGAIN**** (1)
273273
731 | [My Calendar II](https://leetcode.com/problems/my-calendar-ii/) | [Python](./leetcode_python/Array/my-calendar-ii.py) | _O(n^2)_ | _O(n)_ | Medium |`trick`, `basic`| AGAIN** (not start)
274274
747 | [Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/) | [Python](./leetcode_python/Array/largest-number-at-least-twice-of-others.py) | _O(n)_ | _O(1)_ | Easy |`good basic`, `data structure`| OK*
275275
755 | [Pour Water](https://leetcode.com/problems/pour-water/) | [Python](./leetcode_python/Array/pour-water.py) | _O(v * n)_ | _O(1)_ | Medium |`complex`| AGAIN (not start)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241019: 298
1+
20241019: 298,729
22
20241018: 1146
33
20241014: 737
44
20241013: 686,734,737

data/to_review.txt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2024-12-13 -> ['298,729']
12
2024-12-12 -> ['1146']
23
2024-12-08 -> ['737']
34
2024-12-07 -> ['686,734,737']
@@ -9,7 +10,7 @@
910
2024-11-26 -> ['743,889']
1011
2024-11-25 -> ['837']
1112
2024-11-23 -> ['981']
12-
2024-11-22 -> ['1087']
13+
2024-11-22 -> ['298,729', '1087']
1314
2024-11-21 -> ['1146']
1415
2024-11-20 -> ['939']
1516
2024-11-18 -> ['430']
@@ -19,25 +20,25 @@
1920
2024-11-14 -> ['528,334']
2021
2024-11-12 -> ['1145', '753']
2122
2024-11-11 -> ['727']
22-
2024-11-09 -> ['1145,1219']
23+
2024-11-09 -> ['298,729', '1145,1219']
2324
2024-11-08 -> ['1146']
2425
2024-11-06 -> ['524,221,889']
2526
2024-11-05 -> ['743,889']
2627
2024-11-04 -> ['737', '837', '659']
2728
2024-11-03 -> ['686,734,737', '801,552']
2829
2024-11-02 -> ['353', '981', '1057,1066,1110']
29-
2024-11-01 -> ['528,334', '1087']
30+
2024-11-01 -> ['298,729', '528,334', '1087']
3031
2024-10-31 -> ['1146']
3132
2024-10-30 -> ['1145', '939']
3233
2024-10-28 -> ['430']
33-
2024-10-27 -> ['737', '1145,1219', '363']
34+
2024-10-27 -> ['298,729', '737', '1145,1219', '363']
3435
2024-10-26 -> ['1146', '686,734,737', '1032,844,1011']
3536
2024-10-25 -> ['353', '947', '1110, 1055']
36-
2024-10-24 -> ['528,334', '524,221,889']
37+
2024-10-24 -> ['298,729', '528,334', '524,221,889']
3738
2024-10-23 -> ['1146', '743,889']
38-
2024-10-22 -> ['737', '1145', '837', '753']
39-
2024-10-21 -> ['1146', '686,734,737', '727']
40-
2024-10-20 -> ['1146', '353', '981']
39+
2024-10-22 -> ['298,729', '737', '1145', '837', '753']
40+
2024-10-21 -> ['298,729', '1146', '686,734,737', '727']
41+
2024-10-20 -> ['298,729', '1146', '353', '981']
4142
2024-10-19 -> ['1146', '737', '528,334', '1145,1219', '1087']
4243
2024-10-18 -> ['686,734,737', '359,1057,1055(todo)']
4344
2024-10-17 -> ['737', '353', '1145', '939']
@@ -832,19 +833,19 @@
832833
2020-07-10 -> ['734,737', '388', '836,860,863']
833834
2020-07-09 -> ['066,271', '694']
834835
2020-07-08 -> ['066,271', '163', '646', '663']
835-
2020-07-07 -> ['298', '066,271', '210,261', '776', '661,662', '703,787,819']
836+
2020-07-07 -> ['066,271', '210,261', '298', '776', '661,662', '703,787,819']
836837
2020-07-06 -> ['130', '163', '361,393,133,207', '669,682,739,763']
837838
2020-07-05 -> ['163', '734,737', '346,686', '771,775', '701,450', '642,652,657']
838839
2020-07-04 -> ['163', '210,261', '640,645', '545,617,628']
839840
2020-07-03 -> ['361,393,133,207', '482,127,102,107', '762', '606,459']
840-
2020-07-02 -> ['298', '210,261', '734,737', '388', '742', '445,508']
841+
2020-07-02 -> ['210,261', '734,737', '298', '388', '742', '445,508']
841842
2020-07-01 -> ['130', '269', '210,261', '361,393,133,207', '322,380,394']
842843
2020-06-30 -> ['210,261', '361,393,133,207', '734,737', '346,686', '646', '738', '537,553,579', '289,295']
843-
2020-06-29 -> ['298', '361,393,133,207', '734,737', '776', '396', '251,287']
844+
2020-06-29 -> ['361,393,133,207', '734,737', '298', '776', '396', '251,287']
844845
2020-06-28 -> ['130', '734,737', '482,127,102,107', '355,119,536', '231,232,240']
845846
2020-06-27 -> ['298', '346,686', '388', '771,775', '451,414,189', '198,203,230']
846-
2020-06-26 -> ['298', '130', '694', '155,165,167,179']
847-
2020-06-25 -> ['298', '130', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089']
847+
2020-06-26 -> ['130', '298', '694', '155,165,167,179']
848+
2020-06-25 -> ['130', '298', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089']
848849
2020-06-24 -> ['130', '346,686', '388', '776', '742', '661,662', '517,535,529', '153']
849850
2020-06-23 -> ['346,686', '482,127,102,107', '711,046,126,127', '131,134,150']
850851
2020-06-22 -> ['482,127,102,107', '388', '646', '771,775', '738', '701,450', '063,064,120,0146']
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
# Explanation of Overlapping Booking Logic in MyCalendar
3+
4+
This document explains the logic used in the following code snippet from the `MyCalendar` class:
5+
6+
```java
7+
if (start < date.get(1) && end > date.get(0)) {
8+
return false; // There's an overlap
9+
}
10+
```
11+
12+
## Purpose
13+
The purpose of this logic is to check whether a new booking overlaps with an existing booking in the calendar. If the new booking overlaps with an existing booking, the function will return `false`, indicating that the booking cannot be made.
14+
15+
## Visual Explanation
16+
17+
Imagine a timeline where each event has a start and an end time. The new event is represented by `start` and `end`, while an existing event is represented by `date.get(0)` (start) and `date.get(1)` (end).
18+
19+
### Cases:
20+
21+
### 1. **No Overlap - New Event is Completely Before the Existing Event**
22+
23+
```
24+
New: |-----|
25+
Existing: |-----|
26+
```
27+
28+
- **Condition**: `end <= date.get(0)`
29+
- Explanation: The new event ends before the existing event starts, so no overlap.
30+
31+
### 2. **No Overlap - New Event is Completely After the Existing Event**
32+
33+
```
34+
New: |-----|
35+
Existing: |-----|
36+
```
37+
38+
- **Condition**: `start >= date.get(1)`
39+
- Explanation: The new event starts after the existing event ends, so no overlap.
40+
41+
### 3. **Overlap - New Event Partially Overlaps with the Existing Event**
42+
43+
```
44+
New: |-------|
45+
Existing: |------|
46+
```
47+
48+
- **Condition**: `start < date.get(1)` and `end > date.get(0)`
49+
- Explanation: The new event starts before the existing event ends and ends after the existing event starts, causing a partial overlap.
50+
51+
### 4. **Complete Overlap - New Event Starts and Ends Inside the Existing Event**
52+
53+
```
54+
New: |---|
55+
Existing: |-------|
56+
```
57+
58+
- **Condition**: `start < date.get(1)` and `end > date.get(0)`
59+
- Explanation: Both conditions are true, meaning the new event is entirely within the bounds of the existing event.
60+
61+
### 5. **New Event Engulfs the Existing Event (Starts Before and Ends After)**
62+
63+
```
64+
New: |-----------|
65+
Existing: |-----|
66+
```
67+
68+
- **Condition**: `start < date.get(1)` and `end > date.get(0)`
69+
- Explanation: The new event starts before and ends after the existing event, completely overlapping it.
70+
71+
## Breakdown of the Condition:
72+
73+
- **`start < date.get(1)`**: This checks if the new event starts before the existing event ends.
74+
- **`end > date.get(0)`**: This checks if the new event ends after the existing event starts.
75+
76+
If **both conditions are true**, there is an overlap, and the function returns `false` to indicate that the booking cannot be made.
77+
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/my-calendar-i/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.TreeMap;
8+
9+
/**
10+
* 729. My Calendar I
11+
* Medium
12+
* Topics
13+
* Companies
14+
* Hint
15+
* You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.
16+
*
17+
* A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
18+
*
19+
* The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.
20+
*
21+
* Implement the MyCalendar class:
22+
*
23+
* MyCalendar() Initializes the calendar object.
24+
* boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.
25+
*
26+
*
27+
* Example 1:
28+
*
29+
* Input
30+
* ["MyCalendar", "book", "book", "book"]
31+
* [[], [10, 20], [15, 25], [20, 30]]
32+
* Output
33+
* [null, true, false, true]
34+
*
35+
* Explanation
36+
* MyCalendar myCalendar = new MyCalendar();
37+
* myCalendar.book(10, 20); // return True
38+
* myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event.
39+
* myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20.
40+
*
41+
*
42+
* Constraints:
43+
*
44+
* 0 <= start < end <= 109
45+
* At most 1000 calls will be made to book.
46+
*
47+
*/
48+
public class MyCalendar1 {
49+
50+
// V0
51+
// IDEA : BRUTE FORCE (fixed by gpt)
52+
class MyCalendar {
53+
List<List<Integer>> dates;
54+
55+
public MyCalendar() {
56+
this.dates = new ArrayList<>();
57+
}
58+
59+
public boolean book(int start, int end) {
60+
for (List<Integer> date : dates) {
61+
// Check if the new booking overlaps with an existing booking
62+
/**
63+
*
64+
*
65+
* • The condition if (start < date.get(1) && end > date.get(0)) checks if the new event overlaps with any existing booking. Specifically, this checks:
66+
* • start < date.get(1) ensures that the new event starts before the current event ends.
67+
* • end > date.get(0) ensures that the new event ends after the current event starts.
68+
* • If there is an overlap, return false.
69+
* • If no overlap exists, the event is added to the list and returns true.
70+
*
71+
*/
72+
if (start < date.get(1) && end > date.get(0)) {
73+
return false; // There's an overlap
74+
}
75+
}
76+
// If no overlap, add the new booking
77+
List<Integer> newBook = new ArrayList<>();
78+
newBook.add(start);
79+
newBook.add(end);
80+
this.dates.add(newBook);
81+
return true;
82+
}
83+
}
84+
85+
// V1-1
86+
// IDEA : BRUTE FORCE
87+
// https://leetcode.com/problems/my-calendar-i/editorial/
88+
public class MyCalendar_1_1 {
89+
List<int[]> calendar;
90+
91+
MyCalendar_1_1() {
92+
calendar = new ArrayList();
93+
}
94+
95+
/**
96+
* 10 20 iv
97+
* 15 25 start, end
98+
*
99+
*/
100+
public boolean book(int start, int end) {
101+
for (int[] iv: calendar) {
102+
if (iv[0] < end && start < iv[1]) {
103+
return false;
104+
}
105+
}
106+
calendar.add(new int[]{start, end});
107+
return true;
108+
}
109+
}
110+
111+
// V1-2
112+
// IDEA : Sorted List + Binary Search
113+
// https://leetcode.com/problems/my-calendar-i/editorial/
114+
public class MyCalendar_1_2 {
115+
TreeMap<Integer, Integer> calendar;
116+
117+
MyCalendar_1_2() {
118+
calendar = new TreeMap();
119+
}
120+
121+
public boolean book(int start, int end) {
122+
Integer prev = calendar.floorKey(start),
123+
next = calendar.ceilingKey(start);
124+
if ((prev == null || calendar.get(prev) <= start) &&
125+
(next == null || end <= next)) {
126+
calendar.put(start, end);
127+
return true;
128+
}
129+
return false;
130+
}
131+
}
132+
133+
// V2
134+
}

0 commit comments

Comments
 (0)