• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
August 21, 2024 |90 Views

Merge Overlapping Intervals

  Share   Like
Description
Discussion

Merging Intervals

Merging intervals is a classic problem in algorithms and data structures. The problem involves taking a collection of intervals and merging all overlapping intervals into a single interval. This problem is commonly encountered in coding interviews and has practical applications in areas like scheduling and range management.

Problem Statement

Given a set of intervals, merge all overlapping intervals. The goal is to produce a list of merged intervals where no two intervals overlap.

Example

Input: [[1, 3], [2, 6], [8, 10], [15, 18]]

Output: [[1, 6], [8, 10], [15, 18]]

Explanation: Intervals [1, 3] and [2, 6] overlap, so they are merged into [1, 6].

Key Concepts Covered

  • Sorting: The intervals are sorted based on the start time, which makes it easier to merge overlapping intervals.
  • Iterative Merging: You iterate through the sorted intervals and merge them if they overlap.

Steps to Solve the Merging Intervals Problem

Sort the Intervals:

  • Sort the intervals based on their start times. This allows you to easily identify overlapping intervals during iteration.

Iterate and Merge:

  • Initialize an empty list called merged to store the merged intervals.
  • Traverse through each interval:
    • If merged is empty or the current interval does not overlap with the last interval in merged, add the current interval to merged.
    • If the current interval overlaps with the last interval in merged, update the end of the last interval in merged to be the maximum of the two overlapping intervals.

Return the Merged Intervals:

  • Once all intervals are processed, return the merged list containing all the non-overlapping intervals.

Example Implementation in Python

Here’s a simplified approach using Python:

python

Copy code

def merge_intervals(intervals):    # Sort intervals based on the start time    intervals.sort(key=lambda x: x[0])    # Initialize a list to hold the merged intervals    merged = []    for interval in intervals:        # If merged is empty or there's no overlap, add the interval        if not merged or merged[-1][1] < interval[0]:            merged.append(interval)        else:            # If there's overlap, merge the intervals            merged[-1][1] = max(merged[-1][1], interval[1])    return merged # Example usage intervals = [[1, 3], [2, 6], [8, 10], [15, 18]] print(merge_intervals(intervals))  # Output: [[1, 6], [8, 10], [15, 18]]

Applications of Merging Intervals

  • Meeting Room Scheduling: Finding free time slots by merging overlapping booked intervals.
  • Range Management: Merging overlapping ranges in systems like access control or event scheduling.
  • Data Compression: Merging overlapping ranges in storage or network management.

Variations of the Problem

  • Insert Interval: Given a new interval, merge it with an existing set of intervals.
  • Difference of Intervals: Find the intervals that do not overlap with a given range.
  • Interval Intersection: Determine the intersection of two sets of intervals.

Conclusion

The merging intervals problem is a classic example of how sorting and careful iteration can solve complex overlapping scenarios efficiently. This problem is often used in interviews to assess a candidate’s understanding of sorting, iteration, and greedy algorithms.

For a detailed step-by-step guide, check out the full article: https://www.geeksforgeeks.org/merging-intervals/.