Untitled

 avatar
unknown
plain_text
21 days ago
777 B
1
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;
    }
}
Leave a Comment