Design a calendar that allows double bookings but rejects any booking that would create a triple overlap.
My Calendar II
Implement a booking system for a calendar.
You will receive a sequence of interval bookings. Each booking is represented by a start time and an end time, where the interval is treated as half-open: [start, end).
A new booking is accepted if it does not cause any time point to be covered by three or more active bookings. If accepting the booking would create a triple booking at any moment, reject it and keep the calendar unchanged.
Return whether each booking request is accepted or rejected.
Notes
- Intervals are half-open, so a booking ending at time
tdoes not overlap with another booking starting at timet. - A time point may be covered by at most two active bookings.
- The calendar must support multiple booking requests in sequence.
Input Format
- A stream or list of booking requests.
- Each request contains two integers:
startandend. - Process requests in order.
Output Format
- For each request, return
trueif the booking is accepted. - Return
falseif the booking would introduce any triple overlap.
Constraints
0 <= start < end- Intervals are half-open:
[start, end) - The number of requests may be large enough that naive re-checking of all points is inefficient.
Example 1
Input
book(10, 20) -> true book(50, 60) -> true book(10, 40) -> true book(5, 15) -> false book(5, 10) -> true book(25, 55) -> true
Output
[true, true, true, false, true, true]
Explanation
The booking [5, 15) would overlap with both [10, 20) and [10, 40) on [10, 15), creating a triple booking. The others are accepted.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.