Slot a New Booking Into a Sorted Calendar

Implement slotBooking

A calendar holds booked time ranges in sorted order; no two of them overlap or touch. A new booking arrives. Add it to the calendar, combining it with every existing range it overlaps (ranges that merely touch at a single point also count as overlapping), and return the updated calendar, still sorted.

You could throw everything into a list, sort it and merge — but the calendar is already sorted. Because of that, the ranges fall into three groups around the new booking (entirely before it, overlapping it, entirely after it), and one linear pass is enough.

Example 1:

Input: calendar = [[2,4],[6,7],[9,12],[14,16],[19,20]], newBooking = [5,10]

Output: [[2,4],[5,12],[14,16],[19,20]]

Example 2:

Input: calendar = [[5,8]], newBooking = [8,10]

Output: [[5,10]]

Example 3:

Input: calendar = [[5,8]], newBooking = [1,3]

Output: [[1,3],[5,8]]

+ 9 hidden test cases run on Submit.

Constraints:

  • ●1 ≤ calendar.length ≤ 12; each entry is [start, end] with 0 ≤ start < end ≤ 60
  • ●The calendar is sorted by start, and its entries never overlap or touch (each start is strictly greater than the previous end)
  • ●newBooking is [start, end] with 0 ≤ start < end ≤ 60
  • ●Ranges that overlap — including ranges that touch at a single point — are combined. Return the calendar after adding newBooking, still sorted and with no overlapping or touching ranges

calendar =

[[2,4], [6,7], [9,12], [14,16], [19,20]]

newBooking =

[5, 10]