Meeting Room II

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 an array of meeting time intervals, where each interval is represented as [start, end].

Each interval signifies the start and end times of a meeting. Your goal is to determine the minimum number of days required to schedule all the meetings without conflicts.

Key Points:

  • Meetings that overlap cannot be scheduled on the same day.
  • Meetings that touch at their boundaries (e.g., [0,8] and [8,10]) are not considered overlapping.

Constraints:

  • 0 <= intervals.length <= 500
  • 0 <= intervals[i].start < intervals[i].end <= 1,000,000

Explanation:

  • day1: (0,40)
    day2: (5,10),(15,20)

Explanation:

  • (0,8),(8,10) is not considered a conflict at 8

Approach to Solve the Problem

The problem can be solved efficiently using the concept of interval graph coloring. Here’s how:

Step 1: Understand the Problem as a Graph

  • Treat each meeting as a node in a graph.
  • Create an edge between two nodes if their intervals overlap.
    For example:
    • (0,40) and (5,10) overlap, so they are connected by an edge.

Step 2: Determine the Minimum Number of Colors

  • The number of colors required to color the graph such that no two adjacent nodes have the same color is equal to the graph’s chromatic number.
    In our case, the chromatic number corresponds to the minimum number of days required to schedule all meetings without conflicts.

Step 3: Leverage a Sweep Line Algorithm

Instead of explicitly building a graph, we can use a sweep line algorithm to compute the maximum number of overlapping intervals at any given time. This number directly translates to the minimum number of days needed.

Algorithm

  • Extract Start and End Times:
    Create two separate lists:

    • A list of all start times, marked as +1 to indicate an active meeting.
    • A list of all end times, marked as -1 to indicate a meeting ending.
  • Sort the Events:
    Sort all events (start and end times) by time. If two events occur at the same time, prioritize the end event over the start.

  • Track Overlaps:
    Use a counter to track the number of active meetings at any point. The maximum value of this counter gives the number of days req

There are mainly Four  approach to solve this problem – 

  1. Min Heap
  2. Sweep Line Algorithm
  3. Two Pointers
  4. Greedy

1. Min Heap 

  • Time complexity: O(nlogn)
  • Space complexity:  O(n)

2. Sweep Line Algorithm

Time & Space Complexity
  • Time complexity: O(nlogn)
  • Space complexity: O(n)

Code

3. Two Pointers 

  • Time complexity: O(nlogn)
  • Space complexity: O(n)

4. Greedy

  • Time complexity: O(nlogn)
  • Space complexity: O(n)

More Articles