My Calendar II

Implement a calendar that allows double bookings but never a triple booking. An event is a half-open interval [startTime, endTime). MyCalendarTwo() creates an empty calendar, and book(startTime, endTime) adds the event and returns true if no moment would then be covered by three events; otherwise it returns false and leaves the calendar unchanged.

30:00

My Calendar II
medium
Free written solution
Topics
Companies

Implement a calendar that allows double bookings but never a triple booking. An event is a half-open interval [startTime, endTime). MyCalendarTwo() creates an empty calendar, and book(startTime, endTime) adds the event and returns true if no moment would then be covered by three events; otherwise it returns false and leaves the calendar unchanged.

Example 1:
Input: {"commands":["MyCalendarTwo","book","book","book","book","book","book"],"values":[[],[10,20],[50,60],[10,40],[5,15],[5,10],[25,55]]}
Output: [null,true,true,true,false,true,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":["MyCalendarTwo","book","book","book","book","book","book"],"values":[[],[10,20],[50,60],[10,40],[5,15],[5,10],[25,55]]}
new
book(10,20)
book(50,60)
book(10,40)
book(5,15)
book(5,10)
book(25,55)

6 booking requests. Keep every event, plus a second list of the regions already covered twice.

timeline: a moment may hold two events, never three

requestdouble booked (overlaps)none yetbooked eventsno events yet560

request

meets request

new overlap

triple

overlaps list (0)

empty

Returned values

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