My Calendar I

Implement a calendar that accepts a new event only when it does not cause a double booking. An event is a half-open interval [startTime, endTime), so an event ending at 20 and another starting at 20 do not overlap. MyCalendar() creates an empty calendar, and book(startTime, endTime) adds the event and returns true if it overlaps no existing event; otherwise it returns false and leaves the calendar unchanged.

30:00

My Calendar I
medium
Free written solution
Topics
Companies

Implement a calendar that accepts a new event only when it does not cause a double booking. An event is a half-open interval [startTime, endTime), so an event ending at 20 and another starting at 20 do not overlap. MyCalendar() creates an empty calendar, and book(startTime, endTime) adds the event and returns true if it overlaps no existing event; otherwise it returns false and leaves the calendar unchanged.

Example 1:
Input: {"commands":["MyCalendar","book","book","book"],"values":[[],[10,20],[15,25],[20,30]]}
Output: [null,true,false,true]
Constraints:
  • 0≤startTime<endTime≤1090 \leq \text{startTime} < \text{endTime} \leq 10^9

  • At most 10001000 calls will be made to book.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"commands":["MyCalendar","book","book","book"],"values":[[],[10,20],[15,25],[20,30]]}
new
book(10,20)
book(15,25)
book(20,30)

3 booking requests. Accepted events are kept sorted by start time.

timeline: events are half-open [start, end)

requestbooked1030no events yet

request

neighbour

comparing

overlap

events sorted by start

empty

Returned values

[]
Variables
VariableValue
booked0
output[]
DepthFunction Call
Stack empty
0/14