teacher
Sign in

Sweep Line and Intervals

Turn intervals into start and end events, sort them, and sweep left to right to count overlaps, merge ranges and handle huge coordinates.

40 minCF 1000–17004 interactive9 problems

The problem

You have nn intervals on a number line, for example meetings with a start and end time. Questions of this kind come up again and again:

  • What is the largest number of intervals that overlap at one moment?
  • If I merge all overlapping intervals, what remains?
  • For each kk, how many points are covered by exactly kk intervals?
1 of 10