Find Right Interval

To solve this coding challenge, we need to find the smallest interval starting point that is greater than or equal to the ending point of each given interval in an array. If such an interval does not exist, return -1 for that interval. The solution uses sorting and binary search for efficient interval searching.

Explanation:

  1. Problem Understanding : Each interval
    i
    = [starti, endi] has a unique start point. For each interval, we need to find another interval
    j
    such that startj >= endi, and startj is the smallest possible value meeting this condition. If no such interval exists, we return -1.
  2. Examples Breakdown :
    • Example 1:
      intervals = [[1, 2]]
      . Because there is only one interval, no right interval exists, and the output is
      [-1]
      .
    • Example 2:
      intervals = [[3, 4], [2, 3], [1, 2]]
      . Here:
      • For [3, 4], no interval starts at or after 4, so the result is -1.
      • For [2, 3], the interval [3, 4] (index 0) starts at 3 which is >= 3, so the result is 0.
      • For [1,2], the interval [2,3] (index 1) is the right interval starting at 2 which is the smallest start >= 2, so the result is 1.
    • Example 3:
      intervals = [[1, 4], [2, 3], [3, 4]]
      . Here:
      • For [1,4], no interval starts at or after 4, so the result is -1.
      • For [2, 3], the interval [3,4] (index 2) starts at 3 which satisfies the requirement, so the result is 2.
      • For [3, 4], no interval starts at or after 4, so the result is -1.
  3. Approach :
    • First, sort the intervals based on the start points while keeping track of their original indices.
    • For each interval, perform a binary search on the sorted start points to find the smallest start point greater than or equal to the end point of the current interval.
    • Append the original index of the found interval to the result list, or append -1 if no such interval is found.

    Detailed Steps in Pseudocode:

  4. Sort the intervals by start points with their indices :
    • Create a list of tuples containing (start_point, index).
    • Sort this list based on the start_point.
  5. Initialize the result list :
    • Create an empty list to store results.
  6. Binary search for each interval's right interval :
    • For each interval in the input intervals,
    • Define the target as the end point of the current interval.
    • Using binary search, determine the smallest start point in the sorted list that is greater than or equal to the target.
      • Initialize binary search bounds:
        left
        to 0,
        right
        to the length of sorted list - 1.
      • While
        left
        is less than or equal to
        right
        :
        • Compute mid-point.
        • If sorted[mid].start is less than target, adjust
          left
          .
        • Otherwise, adjust
          right
          .
      • After the loop, check if the index
        left
        is within bounds.
        • If true, append the index of that start point to result.
        • Otherwise, append -1.
  7. Return the result list .
The pseudocode implementation along with comments explaining each step is as follows:
                                            
# Pseudocode function to find right intervals

# Function definition
function findRightInterval(intervals):
    # Step 1: Sort intervals based on start points
    start_sorted = sort([(interval[0], index) for index, interval in enumerate(intervals)]) 
    # 'start_sorted' is a list of tuples (start_point, original_index)
    
    result = [] # Initialize result list
    
    # Step 2: Binary search for each interval
    for interval in intervals:
        target = interval[1]  # End point of the current interval
        left = 0  # Initialize binary search bounds
        right = length(start_sorted) - 1
        
        # Binary search loop
        while left <= right:
            mid = left + (right - left) // 2
            if start_sorted[mid][0] < target: # If mid start point is less than target
                left = mid + 1  # Move left bound up
            else: # If mid start point is greater than or equal to target
                right = mid - 1  # Move right bound down
        
        # After binary search, check if we found valid interval
        if left < length(start_sorted):
            result.append(start_sorted[left][1]) # Append found interval's original index
        else:
            result.append(-1) # Append -1 if no interval found
    
    return result # Return the list of results

                                        
In this solution, sorting the intervals by their starting points and using binary search helps efficiently find the right intervals with a time complexity of O(n log n) due to sorting and searching operations, where
n
is the number of intervals.

Keep in mind that topics like sorting algorithms, binary search, and tuple handling must be clearly understood to grasp the complete functionality of this solution. This method ensures computational efficiency, making the approach suitable for handling large data sets as specified in the constraints.