insert-interval

Interval"

Since we only have to insert one interval here, and our intervals are already sorted, we can do this in O(n) time by thinking about it as having three sections:

  1. All intervals that come before the new interval (we'll call this left)
  2. All intervals that come after the interval (we'll call this right).
  3. All intervals that overlap with the new interval
def main(intervals, newInterval):
  left = []
  right = []
  mid = [newInterval[0], newInterval[1]]

  for interval in intervals:
      if interval[1] < start:
          left.append(interval)
      elif interval[0] > end:
          right.append(interval)
      else:
          mid[0] = min(mid[0], interval[0])
          mid[1] = max(mid[1], interval[1])

  return left + [mid] + right