Skip to content

Commit dc131a5

Browse files
committed
Two-Hundred-Thirty-Nine Commit: Add My Calendar I problem to Ordered Set Section
1 parent 1948a0f commit dc131a5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Ordered_Set;
2+
3+
// Problem Statement: My Calendar I (medium)
4+
// LeetCode Question: 729. My Calendar I
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.TreeSet;
9+
10+
public class Problem_3_My_Calendar_1 {
11+
private static TreeSet<int[]> bookings = new TreeSet<>((a, b) -> a[0] - b[0]);
12+
13+
public static List<Boolean> book(int[][] nums) {
14+
List<Boolean> results = new ArrayList<>();
15+
16+
for (int[] interval : nums) {
17+
int start = interval[0];
18+
int end = interval[1];
19+
int[] event = new int[] { start, end };
20+
int[] lower = bookings.lower(event); // Find the closest event that starts before this one
21+
int[] higher = bookings.higher(event); // Find the closest event that starts after this one
22+
23+
// Check for overlap with neighboring events
24+
if ((lower == null || lower[1] <= start) && (higher == null || end <= higher[0])) {
25+
bookings.add(event); // Add to TreeSet if no overlap
26+
results.add(true);
27+
} else {
28+
results.add(false);
29+
}
30+
}
31+
return results;
32+
}
33+
}

0 commit comments

Comments
 (0)