Loading section...
Sweep Line: Events Drive Everything
Concepts: pySweepLine, pyCoverageGaps, pyEventSort
The sweep line algorithm is the generalization of the two-pointer meeting rooms approach. Convert every interval into two events: a +1 at the start and a -1 at the end. Sort all events by time (breaking ties: end events before start events at the same time — more on this shortly). Sweep through events, maintaining a running count. The running count at any moment is the number of active intervals. From this single sweep you can read off: maximum overlap, total covered time, gap locations, and coverage histogram. Core Sweep Line Implementation The tie-breaking rule (-1 before +1 at the same timestamp) is subtle but important. If an interval ends at time 5 and another starts at time 5, should they overlap? If you process the start before the end, they briefly show as 2 active, and you might c