Ctrl K

Merging Room Bookings

Medium
A meeting room has been booked several times. Booking bookings[i] = [start, end] occupies the time from start to end, both ends included. Bookings may overlap. Combine every group of overlapping bookings: two segments that share at least one point (including when one ends exactly where the other begins) become a single segment. Return the resulting non-overlapping segments sorted by start. Together they must cover exactly the same time as the original bookings.
Example 1
bookings=[[9, 12], [1, 4], [3, 5], [12, 14]] → [[1, 5], [9, 14]]

[1, 4] and [3, 5] overlap and become [1, 5]. [9, 12] and [12, 14] touch at 12, so they merge into [9, 14] as well. The result is sorted by start.

Example 2
bookings=[[2, 10], [3, 4], [6, 7]] → [[2, 10]]

Both short bookings lie entirely inside [2, 10].

Example 3
bookings=[[5, 6], [0, 2]] → [[0, 2], [5, 6]]

The bookings do not overlap, so they stay as they are, just ordered by start.

Constraints
  • 1 ≤ length of bookings ≤ 10⁴
  • each bookings[i] has exactly two numbers
  • 0 ≤ start ≤ end ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsZero-Sum Triples MediumTriple Sum Nearest the Target MediumFour Numbers to a Target MediumDistinct Orderings With Repeats MediumAnagram Buckets Medium
loading Python…
Input
bookings=[[9, 12], [1, 4], [3, 5], [12, 14]]
Expected
[[1, 5], [9, 14]]

Run checks the first 3 tests in your browser. Submit checks all 94 tests on the server, hidden ones included.