Collapse Booking Windows into Non-Touching Blocks
Greedy & IntervalsAuto-graded · 21 test casesLast updated
Merge inclusive ranges that share at least one point into a sorted list of disjoint ranges, testing sorting, a linear sweep, and correct handling of touching endpoints and nesting.
Collapse Booking Windows
A scheduling tool stores reserved time windows as inclusive ranges of integers. Each window is a pair [lo, hi] that covers every integer value from lo through hi, including both ends.
Given a list of such windows in any order, combine windows that share at least one value so that the result contains only windows that are pairwise disjoint. The result must cover exactly the same values as the input, and be ordered by ascending lo.
Rule for combining
Take two windows [a, b] and [c, d] where a <= c. They are combined if and only if c <= b, that is, when they have at least one value in common.
- Windows that touch at a single shared value, such as
[1, 3]and[3, 5], are combined into[1, 5]. - Windows that sit next to each other without sharing a value, such as
[1, 2]and[3, 4], stay separate. - A window fully inside another is absorbed by it.
- Identical windows collapse into one.
- Windows with the same
lobut differenthiare combined, keeping the largerhi.
Task
Implement collapse_windows(windows) which returns the list of combined, pairwise disjoint windows sorted by lo.
- If the input is empty, return an empty list.
- Each returned window is a two-element list
[lo, hi].
Constraints
0 <= len(windows) <= 100_000- Each window has exactly two integers:
windows[i] = [lo, hi] -10^9 <= lo <= hi <= 10^9- The input windows may appear in any order.
Examples
Example 1
Input: collapse_windows([[1, 3], [8, 10], [2, 6], [15, 18]])
Output: [[1, 6], [8, 10], [15, 18]]
Example 2
Input: collapse_windows([[1, 4], [4, 5]])
Output: [[1, 5]]
Example 3
Input: collapse_windows([[1, 2], [3, 4]])
Output: [[1, 2], [3, 4]]
Hints
Hint 1
If the windows were ordered by their start value, which windows could possibly merge with the one you are currently building?
Hint 2
Sweep once over the sorted windows while keeping the last combined block. Compare the next start with that block's end, and think about whether the comparison should be strict or not given that shared endpoints count as overlap.
Hint 3
When extending the current block, set its end to the maximum of the old end and the new end, so a window nested inside the block does not shrink it.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sort the windows by their start value. After sorting, any window that can merge with the block currently being built appears right after it, so a single left-to-right pass is enough.
Keep a list result whose last element is the block under construction. For each next window [lo, hi]:
- If
lo <= result[-1][1], the window shares at least one value with the current block. Extend the block's end tomax(current_end, hi). - Otherwise there is a real gap. Append
[lo, hi]as a new block.
The comparison must be <= because windows are inclusive: [1, 4] and [4, 5] share the value 4 and must merge, while [1, 2] and [3, 4] share nothing and stay apart. The max is needed so that a nested window such as [2, 5] inside [1, 10] does not reduce the block's end.
Why it is correct
Invariant: after processing the first k sorted windows, result is a sorted list of disjoint blocks whose union equals the union of those k windows.
- Starts are non-decreasing, so a new window can only reach the last block. Earlier blocks ended before the last block began.
- If
lois greater than the last block's end, every later window starts at or afterlo, so the last block is final and can never be touched again. - If the window overlaps, the union of the two is exactly
[block_start, max(block_end, hi)], and the block start does not change because sorting guaranteesblock_start <= lo.
Windows with equal starts always overlap, and max picks the larger end regardless of their order, so sorting by start alone suffices. Neighboring output blocks have a genuine gap, so no further merging is possible.
Complexity
- Time: O(n log n) for the sort plus an O(n) sweep.
- Space: O(n) for the sorted copy and the output. The input is never mutated, since the first window is copied into a fresh list.
Reference solution
from typing import List
def collapse_windows(windows: List[List[int]]) -> List[List[int]]:
if not windows:
return []
ordered = sorted(windows, key=lambda w: w[0])
result = [[ordered[0][0], ordered[0][1]]]
for lo, hi in ordered[1:]:
if lo <= result[-1][1]:
if hi > result[-1][1]:
result[-1][1] = hi
else:
result.append([lo, hi])
return result