Overview
Merge overlapping and back-to-back room bookings with one sort and one pass, and keep a long booking whole when a short one sits inside it.
- sort by start first
- extend the last block or start a new one
- keep max(end, new_end)
Short Course
Sort intervals by start; then each one either overlaps the last merged block
or starts a new one. One sort plus one pass: O(n log n). Trap: a booking
inside a longer one must not shrink it, so keep max(end, new_end).
a, b = (1, 4), (3, 6)
b[0] <= a[1] # True: b starts before a ends
(a[0], max(a[1], b[1])) # (1, 6): one block
Task
Write merge_bookings(bookings): merge overlapping or back-to-back
(start, end) bookings and return them as (start, end) tuples, sorted by start. Example:
merge_bookings([(5, 8), (1, 3), (2, 4)]) → [(1, 4), (5, 8)].
Test Suites
Pending
Next
Output