Non-overlapping Intervals (Deprecated)

Given a collection of intervals, find the number of intervals you need to remove to make the rest of the intervals non-overlapping. Tags: Greedy, Sorting

Try It!

Discussion

Video

Solution

class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        intervals.sort(key=lambda x: x[0])
        
        count = 0
        prev = intervals[0]
        for i in range(1, len(intervals)):
            cur = intervals[i]
            if prev[1] > cur[0]:
                count += 1
                prev[1] = min(prev[1], cur[1])
            else:
                prev = cur
                
        return count