In this problem, the goal is to implement a booking system where events can be added if they do not result in more than two overlapping events. If a triple booking (three overlapping events) occurs, the booking is rejected. Below is a step-by-step explanation of the logic behind the code for each language.
- Loop through the
double_booked
intervals, which store already double-booked ranges. - If the new event overlaps with any double-booked range, it would cause a triple booking.
- If such an overlap is found, return
false
to reject the booking.
- Loop through the
single
booked intervals. - If the new event overlaps with a single-booked event, calculate the overlap and store it in
double_booked
to keep track of double bookings.
- If no triple booking is detected, add the new event to
single
booked intervals.
- Return
true
to indicate the event has been successfully booked.
- Iterate through
doubleBooked
, the list storing intervals that have already been booked twice. - If the new event overlaps with any interval in
doubleBooked
, returnfalse
to reject the booking due to potential triple booking.
- Loop through
single
bookings, and if the new event overlaps with any single-booked interval, add the overlapping part todoubleBooked
.
- If the event does not result in a triple booking, add it to the list
single
.
- Return
true
if the booking is successful.
- Loop through the
doubleBooked
array. - Check if the new event overlaps with any already double-booked interval.
- If an overlap is found, return
false
because it would result in a triple booking.
- Loop through
single
bookings. - For each single-booked interval that overlaps with the new event, add the overlapping part to
doubleBooked
.
- Add the new event to the
single
array after ensuring no triple booking occurs.
- Return
true
if the event is successfully booked.
- Loop through the
double_booked
intervals. - If the new event overlaps with any double-booked interval, return
false
because a triple booking would occur.
- Loop through
single
booked intervals. - For any overlap between the new event and existing single bookings, add the overlapping part to
double_booked
.
- If no triple booking is detected, add the event to
single
bookings.
- Return
true
if the booking is successful.
- Iterate through
doubleBooked
intervals. - If the new event overlaps with any double-booked interval, return
false
as it would cause a triple booking.
- Loop through the
single
intervals. - For any overlap with the new event, append the overlapping interval to
doubleBooked
.
- Add the new event to the
single
bookings if no triple booking is detected.
- Return
true
if the event is successfully booked without causing triple bookings.
The logic across all languages remains consistent:
- Check for triple bookings (overlaps in double-booked intervals).
- Store overlaps of new events with single-booked intervals as double bookings.
- Add the new event to single-booked intervals if no triple booking is found.
- Return
true
if the booking is successful, otherwise returnfalse
.