Merge Intervals



John Rankin @ May 10, 2023

The Merge Intervals pattern is commonly used in coding interviews to solve problems involving overlapping or merging intervals. It's particularly useful when dealing with problems related to scheduling, time intervals, or event management. Let me explain the pattern and provide you with an example question and its solution.

Pattern: Merge Intervals

  1. Sort the intervals based on their start time.
  2. Create an empty result list to store the merged intervals.
  3. Initialize the first interval from the sorted list as the current interval.
  4. Iterate through the remaining intervals.
    • If the current interval and the next interval overlap, merge them by updating the end time of the current interval.
    • If they don't overlap, add the current interval to the result list and update the current interval to the next interval.
  5. After the iteration, add the last current interval to the result list.

Now, let's see an example question and its solution using the Merge Intervals pattern.

Example Question: Given a list of intervals representing the start and end time of meetings, find all the intervals when a person is busy, considering that the person can attend only one meeting at a time.

Example: Intervals: [(1, 4), (2, 5), (7, 9)] Output: [(1, 5), (7, 9)] Explanation: The person is busy from 1 to 5 (attending meetings 1 and 2) and from 7 to 9 (attending meeting 3).

Solution:

def merge_intervals(intervals):
    if len(intervals) < 2:
        return intervals

    intervals.sort(key=lambda x: x[0])  # Sort intervals based on start time
    merged = []
    start, end = intervals[0]

    for i in range(1, len(intervals)):
        interval = intervals[i]
        if interval[0] <= end:  # Overlapping intervals
            end = max(end, interval[1])
        else:  # Non-overlapping interval found
            merged.append((start, end))
            start, end = interval

    merged.append((start, end))  # Add the last interval
    return merged

# Testing the function
intervals = [(1, 4), (2, 5), (7, 9)]
print(merge_intervals(intervals))

Output: [(1, 5), (7, 9)]

The solution follows the Merge Intervals pattern by first sorting the intervals based on their start time. Then, it iterates through the sorted intervals, merging the overlapping intervals and adding the non-overlapping intervals to the result list. Finally, it adds the last interval to the result list. In the provided example, the output correctly identifies the busy intervals when the person is attending meetings.

 



 

 

 

 

Most Recent Posts


Bitwise XOR-exclusive OR

Read This
May 10, 2023

0-1 Knapsack

Read This
May 10, 2023

K-Way Merge

Read This
May 10, 2023

Subsets

Read This
May 10, 2023

Backtracking

Read This
May 10, 2023

Greedy Algorithm

Read This
May 10, 2023

Trie

Read This
May 10, 2023

Depth-First Search-DFS

Read This
May 10, 2023

Breadth-First Search-BFS

Read This
May 10, 2023

Binary Search Tree-BST

Read This
May 10, 2023

Top K Elements

Read This
May 10, 2023

Binary Search Pattern

Read This
May 10, 2023

Cyclic Sort pattern

Read This
May 10, 2023

Merge Intervals

Read This
May 10, 2023

Day 7 - DynamoDB - and Working with 2 Services - Lambda

Read This
August 25, 2018

Day 6 - Lambda - Creating an API

Read This
August 23, 2018

AWS - Day 5 - S3 - Simple Storage Service

Read This
August 22, 2018

AWS - Day 4 - AWS CLI Useful Scripts

Read This
August 21, 2018

AWS - Day 3 - Create Container from another container

Read This
August 20, 2018

Day 2 - Docker Intro

Read This
August 17, 2018

AWS - Day1 - Tutorial - Launching my first Docker Container

Read This
August 16, 2018

AWS - Day 1 - Signing up and testing out their tutorials

Read This
August 16, 2018

Dynamic Programming - Edit Distance

Read This
December 19, 2016

Dynamic Programming - Fibonacci

Read This
December 19, 2016