Non Overlapping Intervals

Explanation

To solve this coding challenge, we need to ensure that the remaining intervals after removing some become non-overlapping. The plan of action involves sorting the intervals based on their starting times and then iteratively checking for overlaps to decide which intervals to remove.
Key Steps:
  1. Sorting the Intervals : We first sort the intervals based on their starting times. This helps in easily detecting overlaps as we traverse the intervals sequentially.
  2. Iterating Over Intervals : We then initialize a counter to keep track of the number of intervals we need to remove and set a variable to keep track of the endpoint of the previous interval.
  3. Check for Overlaps : For each interval, starting from the second one, we check if the current interval's start time is less than the previous interval's end time. If it is, it means there is an overlap.
  4. Handling Overlaps : For overlapping intervals, we need to increment our removal counter and update the end time of the interval that we keep. We always retain the interval with the smaller end time to maximize the number of non-overlapping intervals.
  5. Updating Non-overlapping Interval End Time : If there is no overlap, we simply update our endpoint tracker to the current interval's end time.
  6. Detailed Steps in Pseudocode
  7. Initialization :
    • Sort the intervals by their start times.
    • Initialize a
      counter
      for tracking the number of removed intervals.
    • Initialize
      previous_end
      to the end time of the first interval.
  8. Iteration :
    • For each interval starting from the second one:
      • Compare the current interval's start with
        previous_end
        .
      • If there's an overlap, increment
        counter
        and update
        previous_end
        to the minimum of
        previous_end
        and the current interval's end.
      • If no overlap, update
        previous_end
        to the current interval's end time.
  9. Return the Result :
    • Return the counter value which represents the minimum number of intervals that need to be removed.

Pseudocode

                                            
# Sort the intervals based on their start times
SORT intervals BY start_time

# Initialize count of intervals to remove and the end time of the previous interval
count = 0
previous_end = intervals[0][1]

# Iterate through the sorted intervals starting from the second interval
FOR i FROM 1 TO intervals.length - 1:
    
    # If the current interval starts before the previous interval ends, there is an overlap
    IF intervals[i][0] < previous_end:
        
        # Increment the count of intervals to remove
        count += 1
        
        # Update the end time of the previous interval to the minimum of previous_end and the current interval's end
        previous_end = MIN(previous_end, intervals[i][1])
        
    ELSE:
        # No overlap, update the end time of the previous interval to the current interval's end time
        previous_end = intervals[i][1]
        
RETURN count

                                        
In this pseudocode:
  • SORT intervals BY start_time
    uses a sorting algorithm to arrange the intervals by their starting times so that we can easily check for overlaps in a sequential manner.
  • FOR i FROM 1 TO intervals.length - 1:
    iterates through the intervals starting from the second one because we're using the first interval's end time as the initial reference.
  • IF intervals[i][0] < previous_end:
    checks if the current interval starts before the previous interval ends, indicating an overlap.
  • count += 1
    increments the counter whenever an overlapping interval is found and removed.
  • previous_end = MIN(previous_end, intervals[i][1])
    updates the previous interval's end time to keep the interval with the smaller end, maintaining maximum possible space for future intervals.
  • RETURN count
    gives the final count of intervals that need to be removed to make the rest non-overlapping.
This method ensures we remove the minimum number of overlapping intervals by prioritizing the intervals that end the earliest.