Intervals & Sweep Lines
Interval and sweep-line algorithms process geometric ranges by sorting boundary endpoints along a 1D timeline or sweep coordinate. A virtual sweep line moves across events, maintaining an active state structure to solve interval merging, maximum concurrent overlaps, skyline profiles, and closest-pair problems.
Key Invariants & Rules
- Event sorting: sort events primarily by time/coordinate X; resolve ties deterministically (ends before starts).
- Interval union merges overlapping ranges [s₁, e₁] and [s₂, e₂] where s₂ <= e₁ into [s₁, max(e₁, e₂)].
- Skyline sweep tracks active building heights in a max-priority structure, emitting key points on height change.
- Divide-and-conquer profiles merge sub-problem altitudes chronologically, tracking current max height.
- Active count tracks concurrency: increment on interval start, decrement on interval end.
Must-Know Algorithms
Merges overlapping closed intervals into minimal disjoint coverage.
Finds peak concurrent room usage by sorting half-open boundary events.
Generates silhouette key points from overlapping rectangular building ranges.
Learning Path (4 Lessons)
Self-Check & Concept Review
1. When sorting sweep events for half-open intervals [start, end), how must equal-time start and end events be ordered to measure concurrency accurately?
2. What is the time complexity of merging N intervals after sorting them by start time?
3. In the Skyline problem, when is a new key point (X, H) emitted by the sweep line?
4. In divide-and-conquer Drone Contest profile merging, how are shared event times resolved?
5. In the 1D Closest Pair algorithm, why is sorting the input array sufficient?