Skip to the code
cf.
+25 XP

#583 Insert a Booking Into the Calendar

medium

A room booking page keeps the booked blocks of a room in booked: ranges [start, end] (both included) sorted by start, with gaps between them. A new booking fresh arrives.

Write insertBooking(booked, fresh) that returns the new list of booked blocks, still sorted, where fresh is merged with every block it overlaps or touches (shares an endpoint with).

Example 1

Input: booked = [[1,3],[6,9]], fresh = [2,5]
Output: [[1,5],[6,9]]

[2, 5] overlaps [1, 3], giving [1, 5].

Example 2

Input: booked = [[1,2],[3,5],[6,7],[8,10],[12,16]], fresh = [4,8]
Output: [[1,2],[3,10],[12,16]]

[4, 8] joins [3, 5], [6, 7], and [8, 10].

Constraints

  • 0 <= booked.length <= 100000
  • booked is sorted by start, and no two blocks overlap or touch.
  • 0 <= start <= end <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).