Leetcode Daily Question 12/10/2024 - 2406. Divide Intervals Into Minimum Number of Groups

Problem Description:

You are given a 2D integer array intervals where intervals[i] = [lefti, righti] represents the inclusive interval [lefti, righti]. | ——————————————————————— | You have to divide the intervals into one or more groups such that each interval is in exactly one group, and no two intervals that are in the same group intersect each other. | ——————————————————————— | Return the minimum number of groups you need to make. | ——————————————————————— | Two intervals intersect if there is at least one common number between them. For example, the intervals [1, 5] and [5, 8] intersect

Solution:

from typing import List
class Solution:
  def minGroups(self, intervals: List[List[int]]) -> int:
    if len(intervals) == 1 or not intervals:
      return 1
    
    states = []
    
    for left, right in intervals:
      states.append((left, 1))
      states.append((right + 1, -1))
      
    states.sort()
    
    max_intersection = 0
    curr_intersection = 0
    
    for _, state in states:
      curr_intersection += state
      max_intersection = max(max_intersection, curr_intersection)
    
    return max_intersection

Explanation:

To find out how many non intersecting groups we can split the intervals into, we can consider the longest chain of intersecting intervals. This is because, these intervals must be in distinct groups, so we must have at most that number of groups.

We do this using the sweep line algorithm. That is we treat these intervals as start and end events and calculate the largest number of concurrent events by assigning states to represent a change in the system.

So, for example, consider [1,5], [2,3], [6,8].

We can assign +1 to the start/ opening of an interval, and -1 otherwise.
We also extend the right end of the interval since intervals are closed on both ends.
This deal with the situation where the right of an interval matches with the left of another.
state = [(1,1),(6,-1),(2,1),(4,-1),(6,1),(9,-1)]
Sorting this by time and priortising the closing/ ending interval if
the time/ number between two intervals match.
state = [(1,1),(2,1),(4,-1),(6,-1),(6,1),(9,-1)]
Going through the states, we calculate the number of intersecting intervals
by incrementing/ decrementing the count. 
The max number achieved is 2. Corresponding to [1,5], [2,3] intersecting but not [6,8].

Putting these ideas into code.

def minGroups(intervals):
  if len(intervals) == 1 or not intervals:
    return 1 
  
  states = []
  
  for left, right in intervals:
    states.append((left, 1))
    states.append((right + 1, -1))
    
  states.sort()
  
  max_intersection = 0
  curr_intersection = 0
  
  for _, state in states:
    curr_intersection += state
  	max_intersection = max(max_intersection, curr_intersection)

The time complexity is O(n log n) as we need to use Python’s modified quick sort to sort the states.

The space complexity is O(n) as we need to store the states of all intervals which takes O(2n) ≈ O(n) space.

The full solution can be found near the top of the page.

Back