Collapse Booked Time Slots into Disjoint Blocks
Greedy & IntervalsAuto-graded · 22 test casesLast updated
Combine a list of [start, end] ranges so that any that overlap or touch become one range, using sorting and a single sweep.
Collapse Booked Time Slots into Disjoint Blocks
You are given a list of booked time slots, where each slot is a two-element list [start, end]. Slots may appear in any order and may overlap one another.
Produce the smallest list of disjoint blocks that covers exactly the same time as the input slots. Two slots belong in the same block if they overlap, or if one ends at the exact moment the other begins (a shared endpoint counts as overlapping). For example, [1, 4] and [4, 5] become [1, 5].
Output rules
- Return a list of
[start, end]blocks. - Blocks must be sorted by
startin ascending order. - No two returned blocks may overlap or touch.
- If the input is empty, return an empty list.
- A slot completely inside another slot is absorbed by it (for example,
[2, 5]inside[1, 10]yields[1, 10]). - Duplicated slots collapse into one.
Function
Implement collapse_slots(slots), which takes the list of slots and returns the list of merged blocks.
Constraints
0 <= len(slots) <= 10^40 <= start < end <= 10^6- Each slot has exactly two integers,
[start, end]. - The input is not guaranteed to be sorted.
Examples
Example 1
Input: collapse_slots([[1, 3], [2, 6], [8, 10], [15, 18]])
Output: [[1, 6], [8, 10], [15, 18]]
Example 2
Input: collapse_slots([[1, 4], [4, 5]])
Output: [[1, 5]]
Hints
Hint 1
If the slots were ordered by start time, which slots could possibly overlap the block you are currently building?
Hint 2
Sweep through the sorted slots while tracking the most recent merged block. Decide whether the next slot starts at or before that block's end.
Hint 3
When extending a block, take the larger of the two end values so that a slot nested inside the block does not shrink it. Remember that touching endpoints must merge.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sort the slots by start time, then make one pass while maintaining a list of finished and in-progress blocks.
- If the input is empty, return
[]. - Sort the slots by their start value (this produces a new list, so the caller's data is untouched).
- Start the result with a copy of the first slot.
- For each remaining slot
[start, end], comparestartwith the end of the last block:- If
start <= last_end, the slot overlaps or touches the last block, so set the block's end tomax(last_end, end). - Otherwise there is a gap, so append
[start, end]as a new block.
- If
The <= comparison is what makes touching slots merge. The max is needed so that a slot nested inside a larger one does not pull the block's end backward.
Why it works
After processing the first k sorted slots, the result holds exactly the union of those slots as ordered, disjoint blocks. This is true for k = 1. For the next slot, its start is at least every earlier start, so it can only interact with the last block: all earlier blocks end before the last block begins, which is at or before this slot's start. If it reaches the last block, merging with the maximum end preserves the union. If it does not, there is a gap that no later slot (whose starts are all at least as large) can bridge backwards, so the last block is final and the new slot begins a fresh block. By induction the final list is the correct answer, and it comes out sorted by start.
Complexity
- Time: O(n log n) for sorting plus O(n) for the sweep, so O(n log n).
- Space: O(n) for the sorted copy and the output.
Reference solution
from typing import List
def collapse_slots(slots: List[List[int]]) -> List[List[int]]:
if not slots:
return []
ordered = sorted(slots, key=lambda slot: slot[0])
blocks = [[ordered[0][0], ordered[0][1]]]
for start, end in ordered[1:]:
if start <= blocks[-1][1]:
blocks[-1][1] = max(blocks[-1][1], end)
else:
blocks.append([start, end])
return blocks