Leetcode 1200 Solution

This article provides solution to leetcode question 1200 (remove-interval)

https://leetcode.com/problems/remove-interval

Solution

class Solution:
    def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]:
        events = []

        for start, end in intervals:
            events.append((start, -1))
            events.append((end, 1))

        events.append((toBeRemoved[0], 1))
        events.append((toBeRemoved[1], -1))

        events.sort()

        level = 0

        ans = []
        last_start = None
        for t, op in events:
            if level == 0 and op == -1:
                last_start = t
            elif level == -1 and op == 1:
                if t > last_start:
                    ans.append((last_start, t))
            level += op
        return ans