My Calendar I

Medium Intervals Ordered set

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

Input`MyCalendar: book(10, 20), book(15, 25), book(20, 30)`
Output`[true, false, true]`

Bookings are half-open, so [20, 30) does not clash with [10, 20).

Constraints

  • 0 <= start < end <= 10^9
  • At most 1000 calls will be made to book.

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (2)
  1. LeetCode 729: My Calendar Ileetcode.com
  2. Java SE 17 API: TreeMapdocs.oracle.com