[0,2]
[1,3]
[5,6]
[7,8]
0
1
2
3
4
5
6
7
8
[0][1][2][3][4][5][6][7][8]
Sort + one-pass sweep
▸1sort intervals by start2out ← []3cur ← intervals[0]4for b in intervals[1:]:5 if b.start <= cur.end: cur.end = max(cur.end, b.end)6 else: out.append(cur); cur ← b7out.append(cur); return out
state
- sorted[0,2] [1,3] [5,6] [7,8]