Untitled
unknown
plain_text
9 months ago
777 B
4
Indexable
class MyCalendar {
private TreeMap<Integer, Integer> bookings;
public MyCalendar() {
bookings = new TreeMap<>();
}
public boolean book(int start, int end) {
Integer prevStart = bookings.floorKey(start); // Start time of the booking just previous to my current booking
Integer nextEnd = bookings.ceilingKey(start); // Start time of the booking just next to my current booking
// Check whether there is any intersection of the current booking with the previous and the next booking
if ((prevStart == null || bookings.get(prevStart) <= start) &&
(nextEnd == null || end <= nextEnd)) {
bookings.put(start, end);
return true;
}
return false;
}
}
Editor is loading...
Leave a Comment