Search lands in PR-5.1 (Pagefind).

How-to Intermediate

Chapter 1 Updated

My Calendar I

Overlap test per booking: start < existing_end AND end > existing_start.

  • Full 5m
  • Revision 2m
  • Flow 1m

Problem

LeetCode #729 — one-liner.

  • Implement book(start, end) that returns true if the new half-open event can be added without overlapping any existing booking, otherwise false.

Key insight

Half-open overlap check.

  • Overlap formula: start < existing_end AND end > existing_start (strict <, > because [start, end) is half-open).
  • Linear scan over existing bookings per call. BST / SortedList optimisation brings per-call to O(log n).

Solution template

Seven lines.

class MyCalendar:
    def __init__(self): self.bookings = []
    def book(self, start, end):
        for s, e in self.bookings:
            if start < e and end > s:
                return False
        self.bookings.append((start, end))
        return True

Complexity

Per-call cost.

  • Time per book O(n) with a list; O(log n) with a balanced BST / SortedList.
  • Space O(n) — stores all accepted bookings.

Edge cases

One to remember.

  • Adjacent half-open events [10, 20) + [20, 30) — no overlap (20 < 20 is false). That’s the point of half-open semantics.

Comments

Comments are disabled in this environment. Set PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and PUBLIC_GISCUS_CATEGORY_ID to enable.