Skip to content
← All Comparisons

Merge Intervals vs Line Sweep

Quick Answer

Use Merge Intervals when you need to combine overlapping intervals into a non-overlapping set, insert a new interval, or find gaps. Use Line Sweep when you need to count overlapping intervals at any point, find the maximum number of overlaps, or process events that happen at specific coordinates.

Side-by-Side Comparison

AspectMerge IntervalsLine Sweep
ApproachSort by start, merge overlappingCreate start/end events, sort, sweep left to right
Time complexityO(n log n)O(n log n)
Space complexityO(n) for outputO(n) for events array
OutputSet of non-overlapping intervalsCounts, max overlap, or derived values
Best forMerging, inserting, removing intervalsCounting overlaps, scheduling, resource allocation
LimitationsCannot easily count overlaps at a pointDoes not produce merged intervals directly
Key signal"Merge overlapping", "insert interval""Maximum overlap", "min rooms", "max concurrent"

When to Use Merge Intervals

When to Use Line Sweep

Key Differences

What they produce

Merge Intervals produces a new list of intervals (the merged result). Line Sweep produces counts or values derived from processing events in order — it answers questions like "how many intervals are active at time t?"

How they handle intervals

Merge Intervals sorts intervals by start time and iterates, extending the current interval or starting a new one. Line Sweep decomposes each interval into two events (+1 at start, -1 at end), sorts all events, and scans left to right while maintaining a counter.

Extensibility

Line Sweep is more general and extensible. It can be combined with heaps, balanced BSTs, or segment trees for advanced problems (skyline, rectangle unions). Merge Intervals is simpler but limited to producing merged intervals.

Code Comparison

Merge Intervals

intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]

for start, end in intervals[1:]:
  if start <= merged[-1][1]:
    # Overlapping — extend
    merged[-1][1] = max(
      merged[-1][1], end
    )
  else:
    # No overlap — new interval
    merged.append([start, end])

Line Sweep (max overlaps)

events = []
for start, end in intervals:
  events.append((start, +1))
  events.append((end,   -1))
events.sort()

active = 0
max_overlap = 0
for _, delta in events:
  active += delta
  max_overlap = max(
    max_overlap, active
  )

How to Decide

  1. Does the problem ask to "merge overlapping intervals"? Use Merge Intervals.
  2. Does it ask for "minimum meeting rooms", "max concurrent", or "maximum overlap"? Use Line Sweep.
  3. Do you need the resulting list of non-overlapping intervals? Merge Intervals.
  4. Do you need a count or aggregate value derived from overlaps? Line Sweep.
  5. Is the problem 2D (skyline, rectangle union)? Line Sweep with an auxiliary data structure.
  6. Are you inserting a new interval into an existing sorted list? Merge Intervals approach (insert then merge).

Example Problems

Merge Intervals

  • 1.Merge Intervals — Given a list of intervals, merge all overlapping intervals and return the result.
  • 2.Insert Interval — Insert a new interval into a sorted list and merge any resulting overlaps.
  • 3.Interval List Intersections — Find the intersection of two sorted lists of intervals.

Line Sweep

  • 1.Meeting Rooms II — Find the minimum number of conference rooms needed (maximum concurrent meetings).
  • 2.The Skyline Problem — Given building rectangles, find the skyline contour using sweep line with a max-heap.
  • 3.My Calendar — Check if a new event can be booked without causing a triple-booking.