-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.java
30 lines (25 loc) · 950 Bytes
/
solution.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
import java.util.ArrayList;
import java.util.List;
class MyCalendarTwo {
List<int[]> single = new ArrayList<>();
List<int[]> doubleBooked = new ArrayList<>();
public MyCalendarTwo() {
}
public boolean book(int start, int end) {
// Check for triple booking by overlapping with double booked intervals
for (int[] booking : doubleBooked) {
if (Math.max(start, booking[0]) < Math.min(end, booking[1])) {
return false; // Triple booking detected
}
}
// Add overlapping parts to double bookings
for (int[] booking : single) {
if (Math.max(start, booking[0]) < Math.min(end, booking[1])) {
doubleBooked.add(new int[] { Math.max(start, booking[0]), Math.min(end, booking[1]) });
}
}
// Add the new event to single bookings
single.add(new int[] { start, end });
return true;
}
}