My Calendar I
Accept a new event only if it overlaps no booked event, by checking the two booked events next to it.
Write a class MyCalendarIbook(start, end)startendstartend[start, end)
If the new event overlaps no event already booked, book
Example 1
Output: true, false, true
Explanation: [15, 25) overlaps [10, 20). [20, 30) starts exactly when [10, 20) ends.
The Idea
Picture one meeting room and its booking sheet. Someone asks for the room from 10 to 20, the sheet is empty, and the booking goes in. The next person asks for 15 to 25. From 15 to 20 both would need the room, so that request is turned down.
The third request, 20 to 30, is accepted, even though 20 is where the first booking ends. That is what the brackets in [start, end)startend
Look at the picture below. Each row is one call to book
In the top row, the new [15, 25) and the booked [10, 20) share the stretch marked overlap 15 to 20, so book
So when do two events overlap? When each one starts before the other one ends. [15, 25) starts at 15, before 20, and [10, 20) starts at 10, before 25, so they overlap. [20, 30) starts at 20, which is not before 20, so it fits.
With many bookings, must a new event be checked against every one of them? Look at the bottom row again. Booked events never overlap each other, so in order of their starts they also end in order, left to right along the line.
That leaves only two booked events that can clash with a new one, its two neighbours in order of start. The first is the event that starts at or just before the new start, which clashes if it is still running then. The second is the event that starts at or just after the new start, which clashes if it begins before the new event ends. Any event further left ends even earlier, and any event further right starts even later.
Solution Approach
Turning that idea into code means looking at the simple way and why it is slow, then settling two things: how to keep bookings sorted by start, and how to check a new event's two neighbours.
Comparing with every booking
What is the simplest way to answer book
Keeping bookings sorted by start
How do we reach the two neighbours without reading the whole list? Java's TreeMapjava.util
A TreeMapfloorEntry(start)startceilingKey(start)startnull
Checking the two neighbours
Which comparisons decide a booking? The entry from floorEntry(start)startceilingKey(start)end
Both tests are strict, greater than and smaller than, so events that only touch pass. A booked event with exactly the same start is caught by the first test, because floorEntrystart
Java Solution
The class works in these steps:
- The calendar keeps its bookings in a field,
, aevents from each booked start to its end. It starts empty.TreeMap - In
, first look up the event before withbook . Ifbefore = events.floorEntry(start) is notbefore and its end,null , is greater thanbefore.getValue() , return false.start - Next, look up the start of the event after with
. IfafterStart = events.ceilingKey(start) is notafterStart and is smaller thannull , return false.end - If neither check failed, the new event fits: put
andstart intoend and return true.events
The main
import java.util.Map;
import java.util.TreeMap;
public class MyCalendarI {
private final TreeMap<Integer, Integer> events = new TreeMap<>(); // start -> end
public boolean book(int start, int end) {
Map.Entry<Integer, Integer> before = events.floorEntry(start); // starts at or before start
if (before != null && before.getValue() > start) {
return false; // still running when the new event starts
}
Integer afterStart = events.ceilingKey(start); // starts at or after start
if (afterStart != null && afterStart < end) {
return false; // starts before the new event ends
}
events.put(start, end);
return true;
}
static void tryBook(MyCalendarI calendar, int start, int end) {
System.out.println("book(" + start + ", " + end + ") -> " + calendar.book(start, end));
}
public static void main(String[] args) {
MyCalendarI calendar = new MyCalendarI();
tryBook(calendar, 10, 20);
tryBook(calendar, 15, 25);
tryBook(calendar, 20, 30);
tryBook(calendar, 5, 15); // runs into [10, 20)
tryBook(calendar, 5, 10); // ends just as [10, 20) starts
tryBook(calendar, 10, 11); // same start as [10, 20)
tryBook(calendar, 25, 28); // inside [20, 30)
tryBook(calendar, 0, 40); // covers everything
tryBook(calendar, 30, 35); // starts just as [20, 30) ends
System.out.println(calendar.events);
}
}
Running it prints:
book(10, 20) -> true
book(15, 25) -> false
book(20, 30) -> true
book(5, 15) -> false
book(5, 10) -> true
book(10, 11) -> false
book(25, 28) -> false
book(0, 40) -> false
book(30, 35) -> true
{5=10, 10=20, 20=30, 30=35}
The opening three results, true, false, true, match Example 1. Then book(5, 15) -> falseafterStartbook(5, 10) -> trueafterStart
book(10, 11)floorEntry(10)book(0, 40)afterStart
Is the second check really needed? A version of bookbook(10, 20)book(5, 15)book(5, 15) -> true
Complexity
Each call to bookTreeMap
- Time: O(log n) per call for n bookings, because
,floorEntry andceilingKey each walk one path down the red-black tree insideput , a sorted tree kept balanced so that a path has about log n steps.TreeMap - Space: O(n), because the map holds one entry for each of the n accepted bookings, and refused events are never stored.
Ranges kept in a TreeMap