LeetCode(力扣)435. 无重叠区间Python

LeetCode435. 无重叠区间

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/non-overlapping-intervals/
LeetCode(力扣)435. 无重叠区间Python_第1张图片

代码

class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        if not intervals:
            return 0
        intervals.sort(key=lambda x: x[0])
        count = 0

        for i in range(1, len(intervals)):
            if intervals[i][0] < intervals[i - 1][1]:
                intervals[i][1] = min(intervals[i - 1][1], intervals[i][1])
                count += 1
        return count

你可能感兴趣的:(leetcode,python,算法)