Module 12 · Intervals · Merge Intervals

Merge intervals

Sort the ranges by start, then walk them once. currRange keeps taking in the next range while they overlap (append: the smaller start, the larger end); the first range that is separate closes the group, because every range after it starts even later.

Each bar is a range on the number line, one lane per range in sorted order; finished groups slide into the bottom lane. A green band is the part two ranges share, a red one the gap between them.