CodeMosa

Master LeetCode Patterns

IntermediateGeometry & Intervals

Sweep Line

Convert intervals/geometry into events, sort them, and sweep while tracking active state

Time: O(n log n)
Space: O(n)

What is Sweep Line?

#
Definition: Sort start/end events and sweep across the axis while maintaining an active set to answer overlap/geometry queries.

Core Idea

#

Create start/end events (+1/−1 or custom), sort with careful tie-breaks, and maintain active structures during the sweep.

When to Use

#

Use with intervals overlap, skyline, meeting rooms, or planar geometry intersections.

How Sweep Line works

#

Convert to start/end events, sort with careful tie-breaks, and sweep while maintaining active counts or structures for overlaps/skylines.

  • Start/end events
  • Timeline concurrency
  • Plane sweep (x-sorted)

General Recognition Cues

#
General indicators that this pattern might be applicable
  • Start/end events
  • Timeline concurrency
  • Plane sweep (x-sorted)
  • Active set changes at boundaries
#
Curated practice sets for this pattern

Code Templates

#
Convert to start/end events, sort with careful tie-breaks, and sweep while maintaining active counts or structures for overlaps/skylines.
# Sweep Line Template (Endpoint events)
def sweep_max_overlap(intervals):
    events = []  # (x, delta)
    for s,e in intervals:
        events.append((s, 1))
        events.append((e, -1))
    events.sort(key=lambda x: (x[0], x[1]))
    cur = best = 0
    for _,d in events:
        cur += d
        best = max(best, cur)
    return best

Pattern Variants & Approaches

#
Different methodologies and implementations for this pattern
1

Interval Endpoints

#

Treat starts as +1 and ends as −1; track running count

When to Use This Variant
  • Max overlaps
  • Room count
  • Line coverage
Use Case
Meeting rooms, platform count
Advantages
  • Linear scan after sort
  • Simple to implement
  • Handles large n
Implementation
def max_overlap(intervals):
    events = []
    for s, e in intervals:
        events.append((s, 1))
        events.append((e, -1))  # treat end as leaving at time e
    events.sort(key=lambda x: (x[0], x[1]))
    cur = ans = 0
    for _, d in events:
        cur += d
        ans = max(ans, cur)
    return ans
2

Plane Sweep

#

Sort by x-coordinate; maintain active set by y-order

When to Use This Variant
  • Segments/rectangles
  • Intersections
  • Skyline
Use Case
Geometry, skyline, intersections
Advantages
  • Deterministic order
  • Event-driven
  • Scales with n log n
Implementation
# Simplified skyline using events and max-heap
import heapq
def skyline(buildings):
    events = []
    for L, R, H in buildings:
        events.append((L, -H, R))  # start
        events.append((R, 0, 0))   # end
    events.sort()
    res = []
    heap = [(0, float('inf'))]  # (-height, end)
    prev = 0
    for x, negH, R in events:
        while heap and heap[0][1] <= x: heapq.heappop(heap)
        if negH: heapq.heappush(heap, (negH, R))
        cur = -heap[0][0]
        if not res or cur != prev:
            res.append([x, cur])
            prev = cur
    return res
3

Timeline Concurrency

#

Generalized event counts with custom weights and tie rules

When to Use This Variant
  • Calendars
  • Resource usage
  • Throttling
Use Case
Peak concurrency, availability windows
Advantages
  • Flexible events
  • Works with multiple resources
  • Good for analytics
Implementation
def timeline_concurrency(events):
    # events: list of (time, delta)
    events.sort(key=lambda x: (x[0], x[1]))
    cur = best = 0
    for _, d in events:
        cur += d
        best = max(best, cur)
    return best

Common Pitfalls

#
Watch out for these common mistakes
  • Wrong event tie-breaker
  • Off-by-one on closed/open intervals
  • Not removing inactive entries
  • Floating point precision issues

Example Problems

#
Classic LeetCode problems using this pattern

Complexity Analysis

#
Time Complexity
O(n log n)

Sorting events dominates; sweep is linear.

Space Complexity
O(n)

Active structures proportional to overlaps.

Ready to test your knowledge?

Test your pattern recognition with interactive questions