#729

My Calendar I

Medium Ordered map (floor / ceiling)

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Book half-open time intervals [start, end). Reject a booking if it overlaps any existing one.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: accept a booking only if it doesn't overlap an existing one.

Why this works: in a sorted map, only the booking just before and the one just after the new start can clash.

Example: book [10,20] ✓, [15,25] → previous ends at 20 > 15 → ✗, [20,30] ✓

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Scanning every booking — O(n) per call, O(n²) overall. (A segment tree is the heavier tool for the harder versions.)

Solve on LeetCode ↗
Complexity
  • Time: O(log n) per booking.
  • Space: O(n).
Topic: Segment Tree, Fenwick & Sparse Table
🎬 Episodes 98–100: video coming soon