File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments