Merge Intervals - Problem

Imagine you're a calendar app developer who needs to simplify a user's busy schedule. Given an array of time intervals where each interval is represented as [start, end], your task is to merge all overlapping intervals and return a clean, non-overlapping schedule.

For example, if someone has meetings from [1,3] and [2,6], these overlap and should be merged into a single interval [1,6]. Your goal is to take a messy schedule with potentially overlapping time slots and return the minimum number of intervals that cover the same time periods.

Key Points:

  • Two intervals [a,b] and [c,d] overlap if b โ‰ฅ c (they share at least one point)
  • The result should contain no overlapping intervals
  • The order of intervals in the result doesn't matter

Input & Output

example_1.py โ€” Basic Overlapping Intervals
$ Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
โ€บ Output: [[1,6],[8,10],[15,18]]
๐Ÿ’ก Note: Intervals [1,3] and [2,6] overlap (since 2 โ‰ค 3), so they merge into [1,6]. The other intervals [8,10] and [15,18] don't overlap with any others, so they remain unchanged.
example_2.py โ€” Adjacent Intervals
$ Input: intervals = [[1,4],[4,5]]
โ€บ Output: [[1,5]]
๐Ÿ’ก Note: Intervals [1,4] and [4,5] are adjacent (they meet at point 4), which counts as overlapping. They merge into [1,5].
example_3.py โ€” Single Interval
$ Input: intervals = [[1,4]]
โ€บ Output: [[1,4]]
๐Ÿ’ก Note: With only one interval, there's nothing to merge. The result is the same as the input.

Visualization

Tap to expand
๐Ÿ“… Executive Calendar OptimizationBefore: Messy overlapping meetings9 AM10 AM11 AM12 PM1 PM2 PMMeeting A[1,3]Meeting B[2,6]Meet C[8,10]Meeting D[15,18]Algorithm: Sort + Merge overlapping meetings[1,3][2,6]OVERLAP!2 โ‰ค 3, so merge!After: Clean, optimized scheduleMerged Block [1,6][8,10][15,18]โœ… Optimized: 4 meetings โ†’ 3 time blocks!๐Ÿ’ก Key InsightSorting enables local decisions:only check last merged interval!
Understanding the Visualization
1
Messy Schedule
Executive has overlapping meetings: [1,3], [2,6], [8,10], [15,18]
2
Sort Chronologically
Organize meetings by start time - already sorted in this case
3
Smart Merging
Walk through meetings: [1,3] and [2,6] overlap (2 โ‰ค 3), merge to [1,6]
4
Continue Process
[8,10] doesn't overlap with [1,6] (8 > 6), keep separate
5
Clean Schedule
Final schedule: [1,6], [8,10], [15,18] - minimum time blocks!
Key Takeaway
๐ŸŽฏ Key Insight: By sorting intervals by start time first, we can make merging decisions locally - we only need to compare each interval with the last one in our result, not all previous intervals. This transforms an O(nยฒ) problem into an O(n log n) solution!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยณ)

In worst case, we need O(n) iterations, each checking O(n) pairs, with O(n) time to merge

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for temporary variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค intervals.length โ‰ค 104
  • intervals[i].length == 2
  • 0 โ‰ค starti โ‰ค endi โ‰ค 104
  • All intervals are valid (start โ‰ค end)
Asked in
Google 127 Amazon 98 Meta 84 Microsoft 73 Apple 56
128.5K Views
Very High Frequency
~18 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen