You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.

A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events).

The event can be represented as a pair of integers startTime and endTime that represents a booking on the half-open interval [startTime, endTime), the range of real numbers x such that startTime <= x < endTime.

Implement the MyCalendar class:

  • MyCalendar() Initializes the calendar object.
  • boolean book(int startTime, int endTime) Returns true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.

Input & Output

Example 1 — Basic Calendar Operations
$ Input: commands = ["MyCalendar", "book", "book", "book"], parameters = [[], [10, 20], [15, 25], [20, 30]]
Output: [null, true, false, true]
💡 Note: MyCalendar() initializes calendar. book(10,20) returns true (first booking). book(15,25) returns false (overlaps with [10,20)). book(20,30) returns true (no overlap since [10,20) ends at 20).
Example 2 — Adjacent Bookings
$ Input: commands = ["MyCalendar", "book", "book"], parameters = [[], [10, 20], [20, 30]]
Output: [null, true, true]
💡 Note: book(10,20) succeeds. book(20,30) succeeds because intervals [10,20) and [20,30) don't overlap (20 is not included in first interval).
Example 3 — Multiple Overlaps
$ Input: commands = ["MyCalendar", "book", "book", "book", "book"], parameters = [[], [5, 10], [25, 30], [15, 20], [12, 18]]
Output: [null, true, true, true, false]
💡 Note: First three bookings succeed. book(12,18) fails because it overlaps with [15,20): max(12,15) < min(18,20) → 15 < 18 is true.

Constraints

  • 0 ≤ start < end ≤ 109
  • At most 1000 calls will be made to book

Visualization

Tap to expand
My Calendar I - No Double Booking INPUT Commands: MyCalendar() book(10,20) book(15,25) book(20,30) Timeline View: 10 15 20 25 30 [10,20) [15,25) [20,30) Red = Conflict (rejected) Green = Accepted ALGORITHM STEPS 1 Initialize Calendar Create empty event list 2 Check Overlap For each existing event: if start < e.end AND end > e.start 3 If Conflict Found Return false, reject 4 No Conflict Add event, return true Execution Trace: book(10,20): list=[] --> OK book(15,25): 15<20 & 25>10 --> CONFLICT! book(20,30): 20<20? NO --> OK FINAL RESULT Output Array: [null, true, false, true] Breakdown: MyCalendar() --> null book(10,20) --> true book(15,25) --> false book(20,30) --> true Calendar State: Events: [10,20), [20,30) 2 bookings stored Key Insight: Two intervals [s1, e1) and [s2, e2) overlap if and only if: s1 < e2 AND s2 < e1 Half-open intervals: [20, 30) does NOT overlap with [10, 20) because 20 is excluded from [10, 20). Time Complexity: O(n) per booking | Space Complexity: O(n) for storing events TutorialsPoint - My Calendar I | Optimal Solution
Asked in
Google 15 Microsoft 12 Amazon 10 Apple 8
98.5K Views
Medium Frequency
~15 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen