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