CodeMosa

Master LeetCode Patterns

IntermediateGreedy & Sorting

Merge Intervals

Sort intervals by start and combine overlapping ones in a single pass.

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

What is Merge Intervals?

#
Definition: Sort intervals by start and merge any overlap to produce a minimal set of disjoint intervals.

Core Idea

#

After sorting by start time, push the first interval; for each next interval, either merge into the last or append if disjoint.

When to Use

#

Use when consolidating overlaps in schedules, ranges, or timelines.

How Merge Intervals works

#

Sort by start, then greedily merge when current.start ≤ last.end; update end with max end.

  • Intervals with start/end
  • Detect overlaps
  • Return minimal set

Merge Intervals vs alternatives

#

Use this pattern when use when consolidating overlaps in schedules, ranges, or timelines.

General Recognition Cues

#
General indicators that this pattern might be applicable
  • Intervals with start/end
  • Detect overlaps
  • Return minimal set
#
Curated practice sets for this pattern

Code Templates

#
Sort by start, then greedily merge when current.start ≤ last.end; update end with max end.
def merge_intervals(intervals):
    intervals.sort(key=lambda x: x[0])
    merged = []
    for s, e in intervals:
        if not merged or s > merged[-1][1]:
            merged.append([s, e])
        else:
            merged[-1][1] = max(merged[-1][1], e)
    return merged

Pattern Variants & Approaches

#
Different methodologies and implementations for this pattern
1

Merge After Sort

#

Classic approach sorting by start and merging greedily.

When to Use This Variant
  • Overlap if next.start ≤ last.end
Use Case
Consolidate intervals
Advantages
  • Simple
  • O(n log n) dominated by sort
Implementation
def merge_intervals(intervals):
    intervals.sort(key=lambda x: x[0])
    merged = []
    for s, e in intervals:
        if not merged or s > merged[-1][1]:
            merged.append([s, e])
        else:
            merged[-1][1] = max(merged[-1][1], e)
    return merged
2

Line Sweep vs Sort-then-Merge

#

Compare endpoint sweeping for overlap counts vs greedy merge for consolidation

When to Use This Variant
  • Peak concurrency
  • Timeline overlaps
  • Count active intervals
Use Case
Maximum overlap, meeting rooms, calendar concurrency
Advantages
  • Sweeping handles counts/peaks
  • Merge returns consolidated ranges
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

Common Pitfalls

#
Watch out for these common mistakes
  • Not sorting first
  • Incorrect overlap condition
  • Off-by-one on inclusive bounds

Example Problems

#
Classic LeetCode problems using this pattern

Complexity Analysis

#
Time Complexity
O(n log n)

Sorting dominates; merging is linear.

Space Complexity
O(n)

Output list of merged intervals.

Ready to test your knowledge?

Test your pattern recognition with interactive questions