Skip to content
RED SAGSkills
medium30 ptsintervals

Slot a Booking Into the Calendar

A calendar is a list of closed intervals calendar[i] = [start, end], sorted by start, with no two intervals overlapping or touching. A new booking booking = [start, end] arrives.

Insert it and merge any intervals that now overlap or touch (sharing even a single endpoint, like [1,3] and [3,5], counts). Return the updated calendar, still sorted and non-overlapping.

Example 1

Input: calendar = [[1,3],[6,9]], booking = [2,5]
Output: [[1,5],[6,9]]
Explanation: [2,5] overlaps [1,3].

Example 2

Input: calendar = [[1,2],[3,5],[6,7],[8,10],[12,16]], booking = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: [4,8] joins [3,5], [6,7] and [8,10].

Constraints

  • 0 <= calendar.length <= 2 * 10^4
  • 0 <= start <= end <= 10^6
Hint 1

Copy every interval that ends before the booking starts.

Hint 2

Then absorb every interval that starts no later than the booking ends, widening the booking; copy the rest.

Discussion 0

Join free to post in the discussion.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Database Administrator

Run checks your code on 3 sample tests. Submit runs all 9 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.