LeetCode 1288 Remove Covered Intervals (Python)

Posted by 小明MaxMing on October 4, 2020

题目

Given a list of intervals, remove all intervals that are covered by another interval in the list.

Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.

After doing so, return the number of remaining intervals.

解题思路

先对所有区间进行排序,左端点从小到大,右端点从大到小,然后遍历每个区间,如果新的区间不被之前的包含,结果+1

代码

class Solution:
    def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
        intervals.sort(key = lambda x: (x[0], -x[1]))
        res = prev = 0
        for _, r in intervals:
            if r > prev:
                res += 1    
                prev = r
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅