Non-overlapping Intervals

Problem Id: 435 Difficulty: Medium


Intuition

Solution


class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        if not intervals:
            return 0

        intervals.sort()
        _, last = intervals.pop(0)
        ans = 0
        for interval in intervals:
            start, end = interval
            if start < last:
                ans += 1
                last = min(end, last)
            else:
                last = end
        return ans