Implement a calendar class. Method book(start, end) books a new event [start, end). Return true if it can be booked without overlap with existing events, false otherwise.
Use a TreeMap to store booked intervals. For each new event, check the floor entry (might overlap from left) and ceiling entry (might overlap from right).
- TreeMap
calendar. - For book(start,end): find floor key <= start; check if floor.end > start (overlap).
- Find ceiling key >= start; check if ceil.start < end (overlap).
- If no overlap: calendar.put(start,end), return true. Else return false.
- Time Complexity: book: O(log N)
- Space Complexity: O(N)