My Calendar II

MediumOrdered Set · Intervals · Design🧩 Ordered Set (TreeMap)LeetCode #731 ↗Updated

Problem

Implement MyCalendarTwo whose book(start, end) tries to add the half-open event [start, end). Two events may overlap (a double booking), but the call must be rejected (return false, store nothing) if the new event would make some moment covered by three events. Otherwise store it and return true.
Inputbook(10,20) book(50,60) book(10,40)Outputtrue, true, true
Inputbook(5,15) book(5,10) book(25,55)Outputfalse, true, true

0 ≤ start < end ≤ 109  ·  at most 1000 calls to book

Try it yourself

Java
class MyCalendarTwo {
    public boolean book(int start, int end) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
A booking fails exactly when some point of [start, end) is already covered twice. So keep track of the double-booked regions (or the max coverage over a range) — you never need to enumerate triples.
PatternOrdered Set (TreeMap)

Instinct Keys keep arriving or leaving and you must ask "what is the nearest key below/above x?" or "current min/max" each time - reach for TreeMap/TreeSet.

Memory hack A HashMap with a sense of direction - floor looks down, ceiling looks up, and both answer in log time while the data keeps changing.

Open the Ordered Set (TreeMap) guide →

Approaches

Brute ForceCheck Every Pair of Bookings

Time O(n2) per bookSpace O(n)

A triple booking needs two stored events a, b whose common part with the new event is non-empty: max(starts) < min(ends). Try every pair; if none qualifies the event is safe to add.

Java
class MyCalendarTwo {
    private final List<int[]> bookings = new ArrayList<>();

    public boolean book(int start, int end) {
        for (int i = 0; i < bookings.size(); i++) {
            int[] a = bookings.get(i);
            for (int j = i + 1; j < bookings.size(); j++) {
                int[] b = bookings.get(j);
                int lo = Math.max(start, Math.max(a[0], b[0]));
                int hi = Math.min(end, Math.min(a[1], b[1]));
                if (lo < hi) return false;   // all three share [lo, hi)
            }
        }
        bookings.add(new int[]{start, end});
        return true;
    }
}

Direct translation of the definition, but quadratic work per call.

At a glance

LevelApproachTimeSpace
Brute ForceCheck Every Pair of BookingsO(n2) per bookO(n)
BetterBookings + Overlap ListO(n) per bookO(n)
OptimalDynamic Segment Tree (Range Add, Range Max)O(log C) per bookO(n log C)
Common pitfalls
  • Closed-interval checks — [10,20) and [20,30) do not overlap.
  • Recording overlaps before the triple check — a rejected call must leave no trace.
  • Rejecting when the new event overlaps two bookings that do not overlap each other.
Interview tips
  • Clarify half-open intervals and the 109 range (rules out a plain array).
  • Explain why overlap regions are disjoint, so the list stays linear.
  • Mention the k-booking generalisation (My Calendar III) via sweep line or segment tree.
Pattern
Interval coverage counting → accept/reject intervals based on how many times a point is already covered.
My notes saved in this browser