Skip to content
Commitwise
Menu

Sweep line

Processing events (like the starts and ends of intervals) in sorted order while keeping a running state, such as how many are open.

Definition

Instead of comparing every interval with every other one, you turn each interval into a start event and an end event, sort all events by time, and walk through them once. A counter or a heap holds what is currently open. It answers questions like the maximum number of overlapping meetings in O(n log n).

In practice

Meetings 9-10, 9:30-11 and 10:30-12 give events at 9 (+1), 9:30 (+1), 10 (-1), 10:30 (+1), 11 (-1), 12 (-1); the count peaks at 2.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.