Non-overlapping Intervals

Minimizing Interval Removals to Ensure Non-Overlapping Intervals

In problems involving intervals, it’s important to efficiently manage and handle cases where intervals might overlap.

In this article, we’ll look at a problem where the goal is to determine the minimum number of intervals that need to be removed to ensure that the remaining intervals are non-overlapping.

Problem Description

You are given a list of intervals, where each interval is represented by a pair of integers [start, end]. Your task is to find the minimum number of intervals that need to be removed in order to make the remaining intervals non-overlapping.

Key Definitions:

  • Non-overlapping intervals: Two intervals [a, b] and [c, d] are non-overlapping if either:
    • b≤c (the first interval ends before the second starts), or
    • d≤a (the second interval ends before the first starts).
  • Overlapping intervals: Two intervals are overlapping if they share at least one common point. For example:
    • [1, 2] and [2, 3] are non-overlapping, because they only meet at point 2.
    • [2, 4] and [2, 4] are overlapping, because they share the range [2, 3].

Explanation:

  • After [1,4] is removed, the rest of the intervals are non-overlapping.

Explanation:

  • [1,2] and [2,3] overlap, so they are merged into [1,3].

Approach to Solve the Problem

To solve this problem efficiently, we can use a greedy approach. The core idea is to always choose intervals that allow the maximum number of subsequent non-overlapping intervals to remain.

Step-by-Step Plan:

  1. Sort the Intervals:

    • First, sort the intervals by their end time. The reason for this is that choosing intervals that end earlier will leave more room for the subsequent intervals, maximizing the chances of not having overlapping intervals.
  2. Greedy Selection:

    • Iterate through the sorted intervals and always choose the interval that doesn’t overlap with the previously selected interval.
    • If an interval overlaps with the previous one, increment the removal count, because we can’t keep both intervals. Instead, we should keep the interval with the earliest end time.
  3. Counting the Removals:

    • The number of intervals to remove will be equal to the total number of intervals minus the number of intervals that can be kept (non-overlapping).

Algorithm

  1. Sort the intervals based on their start time.
  2. Initialize an empty list merged to store the result.
  3. Iterate through the sorted intervals:
    • If the current interval overlaps with the last merged interval, merge them.
    • Otherwise, add the current interval to the result list.

There are mainly three approach to solve this problem – 

  1. Sorting 
  2. Sweep Line Algorithm
  3. Greedy 

1. Recursion 

  • Time complexity: O(2^n)
  • Space complexity:  O(n)

2. Dynamic Programming (Top-Down)

Time & Space Complexity
  • Time complexity: O(n^2)
  • Space complexity: O(n)

Code

3. Dynamic Programming (Bottom-Up)

  • Time complexity: O(n^2)
  • Space complexity: O(n)

More Articles