-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC731.java
More file actions
35 lines (31 loc) · 1.02 KB
/
LC731.java
File metadata and controls
35 lines (31 loc) · 1.02 KB
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
34
35
/*
* LC731
*/
import java.util.*;
class MyCalendarTwo {
List<int[]> bookings;
TreeMap<Integer, Integer> overlappedMap;
public MyCalendarTwo() {
bookings = new ArrayList<>();
overlappedMap = new TreeMap<>();
}
public boolean book(int start, int end) {
// If event is present in overlapped then return false
Integer prevValue = overlappedMap.lowerKey(end);
if (prevValue != null && start <= overlappedMap.get(prevValue) - 1) {
return false;
} // Insert into bookings and if it is overlapping with any booking then insert
// into overlapped map
for (int booking[] : bookings) {
// booking[0], start
// booking[1], end
int commStart = Math.max(booking[0], start);
int commEnd = Math.min(booking[1], end);
if (commStart < commEnd) {
overlappedMap.put(commStart, commEnd);
}
}
bookings.add(new int[] { start, end });
return true;
}
}