Coalesce Overlapping Ranges Into Disjoint Spans
Greedy & IntervalsAuto-graded · 25 test casesLast updated
Combine unsorted inclusive integer ranges that share at least one point into disjoint spans ordered by start, testing endpoint handling, chained merges, and nesting.
You are given a collection of inclusive integer ranges. Combine all ranges that are connected through shared points, and return the resulting set of non-overlapping ranges.
Each range is a two-element list [start, end] with start <= end. It covers every point from start through end, including both ends.
Task
Implement coalesce_ranges(ranges), which returns the combined ranges in ascending order of start.
Rules
- The input can be in any order. It may contain repeated ranges and ranges fully contained in other ranges.
- Two ranges are connected if they have at least one point in common. Ranges that meet at a single endpoint, such as
[1, 3]and[3, 5], share the point 3 and therefore combine into[1, 5]. Ranges such as[1, 2]and[3, 4]have no point in common, so they remain separate, even though no integer lies strictly between them. - Combining is transitive: if range A is connected to B and B is connected to C, then A, B and C all become one range, even if A and C share no point directly.
- In the returned list, no two ranges share a point, the list is sorted by
startascending, and the union of the returned ranges equals the union of the input ranges exactly. - Each returned range is a list
[start, end].
Constraints
1 <= len(ranges) <= 10^5-10^9 <= start <= end <= 10^9- All values are integers.
Examples
Example 1
Input: coalesce_ranges([[8, 10], [1, 4], [3, 6], [12, 12]])
Output: [[1, 6], [8, 10], [12, 12]]
Example 2
Input: coalesce_ranges([[1, 3], [3, 5], [6, 7]])
Output: [[1, 5], [6, 7]]
Example 3
Input: coalesce_ranges([[2, 9], [3, 4], [5, 5], [1, 2]])
Output: [[1, 9]]
Hints
Hint 1
Think about what order makes it easy to decide whether the next range connects to the ones you have already combined.
Hint 2
After ordering by start, keep one current combined range. Compare the next range's start with the current range's end, and decide carefully between a strict and a non-strict comparison for touching endpoints.
Hint 3
When a range is absorbed into the current one, the new end must be the larger of the two ends, otherwise a nested range would wrongly shrink the current span.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sort the ranges by start. Then sweep once from left to right while maintaining a current combined range, which is the last entry of the result list.
For each next range [s, e]:
- If
s <= current_end, the range shares a point with the current one, so extend the current end tomax(current_end, e). - Otherwise there is a gap, so the current range is finished and
[s, e]becomes the new current range.
The comparison must be <= rather than <, because ranges meeting at one endpoint share that point and must be combined. Ranges like [1, 2] and [3, 4] have 3 > 2, so they stay apart.
Why it is correct
Let the current combined range be [S, E], and the next sorted range be [s, e] with s >= S.
- If
s <= E, thenS <= s <= E, so the pointslies in both. They are connected, and their union is[S, max(E, e)]. A nested range leaves the end unchanged because of themax. - If
s > E, every remaining range starts at or afters, hence afterE, so nothing later can connect to[S, E]. It is final. The new range starts afterE, so the output stays disjoint, and it starts at or afterS, so the output stays sorted.
Chained merges work naturally: a middle range extends the current end far enough to reach a third range even if the first and third do not touch. Coverage is exact since every block is a union of input ranges that are connected to each other.
The result list is built from fresh [s, e] lists, so the caller's input is never modified.
Complexity
- Time: O(n log n) for sorting, plus O(n) for the sweep.
- Space: O(n) for the sorted copy and the output.
Reference solution
def coalesce_ranges(ranges):
ordered = sorted(ranges)
result = []
for s, e in ordered:
if result and s <= result[-1][1]:
if e > result[-1][1]:
result[-1][1] = e
else:
result.append([s, e])
return result