Sweep Line Algorithm
Process interval events sorted by position. Essential for meeting rooms, skyline problems, and rectangle area calculations. Often combined with TreeMap or sorted list.
Sweep Line Algorithm
Process interval events sorted by position. Essential for meeting rooms, skyline problems, and rectangle area calculations. Often combined with TreeMap or sorted list.
Meetings: [0,30], [5,10], [15,20]. Convert to events: start=+1, end=-1. Sort by time.
Meeting rooms
Order the sweep that finds how many rooms a set of meetings needs.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Test your knowledge
In Meeting Rooms II, what do sweep line events represent?
The short version
Imagine a vertical line sweeping across a map from left to right, like a scanner. As it moves, it tracks which intervals it's currently inside. This lets you process all events in sorted order.
In more depth
Events in Motion
Sweep Line converts interval problems into a sequence of point events.
Standard Pattern:
Meeting Rooms Example:
meetings = [[0,30], [5,10], [15,20]]
events = [(0,+1), (5,+1), (10,-1), (15,+1), (20,-1), (30,-1)]
sorted events → sweep → max concurrent = 2 roomsKey Gotchas:
When to reach for this
- Count maximum overlapping intervals
- Find union/intersection of intervals
- 2D geometry with rectangles
- Keywords: 'meeting rooms', 'skyline', 'overlapping intervals'
Interview Questions
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required