Soohan-Kim / Leetcode

0 stars 0 forks source link

[HW1] Non-overlapping Intervals #435 #5

Open Soohan-Kim opened 4 months ago

Soohan-Kim commented 4 months ago

https://leetcode.com/problems/non-overlapping-intervals/description/

Soohan-Kim commented 4 months ago
class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        ret = 0
        intervals.sort(key=lambda x:x[1])
        curEnd = intervals[0][1]

        for (s, e) in intervals[1:]:
            if s >= curEnd:
                curEnd = e
            else:
                ret += 1

        return ret