Combine Overlapping Booking Ranges
Implement combineBookings
A reservation system holds a list of booked time ranges in no particular order. Ranges that overlap — including ranges that merely touch at a single point — should be combined into one larger range that covers them all. Return the resulting combined ranges, sorted by start.
Repeatedly searching for overlapping pairs and merging them works but re-scans the list again and again. If the ranges are sorted by start time first, a single left-to-right sweep can build each combined range one after another.
Example 1:
Input: bookings = [[9,12],[1,4],[4,7],[3,5],[14,16],[11,13]]
Output: [[1,7],[9,13],[14,16]]
Example 2:
Input: bookings = [[2,5],[5,9]]
Output: [[2,9]]
Example 3:
Input: bookings = [[6,9]]
Output: [[6,9]]
+ 9 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ bookings.length ≤ 12; each booking is [start, end] with 0 ≤ start < end ≤ 50, in any order - ●
Two ranges overlap if they share any time, including a single boundary point: [1, 4] and [4, 7] overlap - ●
Combine every group of overlapping ranges into one range covering all of them - ●
Return the combined ranges sorted by start; no two returned ranges overlap or touch
bookings =