Problem statement

https://binarysearch.com/problems/Interval-Carving/

Solution

Equal to Leetcode 1272 Remove Interval.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, intervals, R):
        def remove(A, R):
            if A[1] < R[0]: return [A]
            if A[0] > R[1]: return [A]
            ans = []
            if A[0] < R[0]: ans += [[A[0], R[0]]]
            if A[1] > R[1]: ans += [[R[1], A[1]]]
            return ans
            
        ans = []
        for I in intervals:
            ans += remove(I, R)
                
        return ans