Leetcode 435 python 解题报告

AC代码:

class Solution(object):
    def eraseOverlapIntervals(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: int
        """
        if len(intervals) == 0:
            return 0
        intervals = sorted(intervals, key=lambda Interval: Interval.start)
        prev = 0
        count = 0
        for i in range(1,len(intervals)):
            if intervals[prev].end > intervals[i].start:
                if intervals[prev].end > intervals[i].end:
                    prev = i
                count += 1
            else:
                prev = i
        return count
解题思路:自定义排序函数,按interval的start排序,然后分情况讨论,如果后数与前数是包含关系,则贪心法直接舍弃前数,如果是交的关系,则贪心法舍弃后数。

你可能感兴趣的:(Leetcode)