Problem
Implement a calendar that books half-open intervals [start, end). book returns true and records the interval when it does not overlap any existing booking, and false otherwise.
Original: LeetCode 729: My Calendar I.
Examples
Example 1
`MyCalendar: book(10, 20), book(15, 25), book(20, 30)``[true, false, true]`Bookings are half-open, so [20, 30) does not clash with [10, 20).
Constraints
0 <= start < end <= 10^9- At most
1000calls will be made tobook.
Hints
Hint 1
A booking that starts exactly when another ends is allowed. Overlap is start < otherEnd and end > otherStart.
Hint 2
This is interval conflict detection. A sorted map (or a balanced tree of starts) lets you inspect only the neighbours.
Solution
Tried it yourself? The approach, complexity, and code stay hidden until you choose.
Approach
Brute force. Store a list of bookings and scan all of them on each book call.
Optimal. A TreeMap from start to end: floorEntry for the previous interval, ceilingEntry for the next.
Alternative. A list kept sorted by start, with binary search, is the same idea.
Complexity
- Time
- O(log n)
Each book is O(log n) to locate the neighbours, plus O(n) if a list insert shifts elements, or O(log n) overall with a tree map. Space is O(n).
- Space
- O(n)
Code
from bisect import bisect_left
class MyCalendar:
def __init__(self) -> None:
self.bookings: list[list[int]] = [] # sorted by start
def book(self, start: int, end: int) -> bool:
i = bisect_left(self.bookings, [start, end])
if i and self.bookings[i - 1][1] > start:
return False
if i < len(self.bookings) and self.bookings[i][0] < end:
return False
self.bookings.insert(i, [start, end])
return True
if __name__ == "__main__":
cal = MyCalendar()
assert cal.book(10, 20) is True
assert cal.book(15, 25) is False
assert cal.book(20, 30) is True
print("ok")import java.util.TreeMap;
public class MyCalendarI {
private final TreeMap<Integer, Integer> bookings = new TreeMap<>();
boolean book(int start, int end) {
var prev = bookings.floorEntry(start);
if (prev != null && prev.getValue() > start) return false;
var next = bookings.ceilingEntry(start);
if (next != null && next.getKey() < end) return false;
bookings.put(start, end);
return true;
}
public static void main(String[] args) {
var cal = new MyCalendarI();
if (!cal.book(10, 20)) throw new AssertionError();
if (cal.book(15, 25)) throw new AssertionError();
if (!cal.book(20, 30)) throw new AssertionError();
System.out.println("ok");
}
}Edge cases
- Official sample: book(10, 20) is true, book(15, 25) is false, and book(20, 30) is true because 20 is the previous end.
- Intervals are half-open, so the comparison is strictly greater or strictly less, not >= / <=.
Follow-ups
- My Calendar II allows a single overlap and only rejects a triple booking.
Interview script
"I only need to check the interval immediately before and immediately after the new start. A TreeMap, or a binary search in a sorted list, finds those two neighbours in logarithmic time."
Source: LeetCode 729: My Calendar I Source: Java SE 17 API: TreeMap
Sources (2)
- LeetCode 729: My Calendar Ileetcode.com
- Java SE 17 API: TreeMapdocs.oracle.com