bookings is a list of closed intervals [start, end], sorted by start, pairwise disjoint and non-touching. Insert new = [s, e] and return the resulting calendar as a sorted list of disjoint closed intervals. Two intervals that overlap or touch (share at least one point, e.g. [1, 3] and [3, 5]) must be merged.
Examples
Input: bookings = [[2, 3], [5, 6], [8, 9], [11, 13], [16, 18]], new = [6, 11]
Output: [[2, 3], [5, 13], [16, 18]]
Explanation: [6, 11] touches [5, 6] and [11, 13] and covers [8, 9]; they all become [5, 13].
Input: bookings = [[1, 5]], new = [2, 3]
Output: [[1, 5]]
Constraints
0 <= len(bookings) <= 5 * 10**4,0 <= start <= end <= 10**9.- The result must be a list of lists.
- Return a new list; do not modify
bookings. One test callsbookings_after(bookings, new), which runs youradd_booking, appends an item to the list you returned and then returnsbookingsitself, so it must come back unchanged. - Target complexity: O(n).
Goals
- Split the sorted list into before, overlapping and after parts
- Grow the new interval while absorbing everything it overlaps or touches
- Return a fresh list without mutating the input