SDE & MLE Prep

1288. Remove Covered Intervals

Problem

Interval [a, b) is covered by [c, d) if c ≤ a and b ≤ d. Remove every interval covered by another one and return how many remain.

Example: [[1,4],[3,6],[2,8]] → 2 ([3,6] is covered by [2,8])

Constraints: up to 1000 intervals, all distinct.

Sort by start ascending and end descending (so a longer interval with the same start comes first). Sweep tracking the max end; an interval whose end ≤ max end is covered.

Complexity: O(n log n).

class Solution:
    def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
        keep, max_end = 0, -1
        for s, e in sorted(intervals, key=lambda x: (x[0], -x[1])):
            if e > max_end:
                keep += 1
                max_end = e
        return keep