Problem
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking.
A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).
The event can be represented as a pair of integers start
and end
that represents a booking on the half-open interval [start, end)
, the range of real numbers x
such that start <= x < end
.
Implement the MyCalendarTwo
class:
MyCalendarTwo()
Initializes the calendar object.boolean book(int start, int end)
Returnstrue
if the event can be added to the calendar successfully without causing a triple booking. Otherwise, returnfalse
and do not add the event to the calendar.
Examples
Example 1:
Input
["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
Output
[null, true, true, true, false, true, true]
Explanation
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
myCalendarTwo.book(10, 20); // return True, The event can be booked.
myCalendarTwo.book(50, 60); // return True, The event can be booked.
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking.
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
Solution
Method 1 - Using TreeMap and Count
Visualize this process as “scanning” from left to right with a “vertical laser”. Each endpoint (either a starting point or an ending point) is treated as an event—where a starting point increments by +1 and an ending point decrements by -1. The cumulative value “count” represents the number of “active” intervals intersected by the vertical laser.
Code
Java
class MyCalendarTwo {
private final TreeMap<Integer, Integer> map;
public MyCalendarTwo() {
map = new TreeMap<>();
}
public boolean book(int start, int end) {
map.put(start, map.getOrDefault(start, 0) + 1);
map.put(end, map.getOrDefault(end, 0) - 1);
int count = 0;
for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
count += entry.getValue();
if(count > 2) {
map.put(start, map.get(start) - 1);
if(map.get(start) == 0) {
map.remove(start);
}
map.put(end, map.get(end) + 1);
if(map.get(end) == 0) {
map.remove(end);
}
return false;
}
}
return true;
}
}
Dry Run
Lets take input for book as:
bookings = [[10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
book=[10,20]
map = {10:1, 20:-1}
count = 1+-1 = 0
output = true
book = [50,60]
map = {10:1, 20:-1, 50:1, 60:-1}
count = 1+-1+1+-1
ouptut = true
book=[10,40]
map = {10:2, 20:-1, 40:-1, 50:1, 60:-1}
count = 2 + -1 + -1 + 1+ -1 = 0 ; so Count never exceeds 2
output = true
book = [5,15]
map = {5: 1, 10:2, 15: -1, 20:-1, 40:-1, 50:1, 60:-1}
count = 1+2 ... count exceeds 2, so we cannot add this value
output = false
But because we added the interval earlier, we should remove it as we are returning false.
Observe how the map’s ordering shifts during a dry run. This is why we are utilising a TreeMap. Refer: Java TreeMap Class