435. Non-overlapping Intervals

Doyeon Kim·2022년 9월 9일

코딩테스트 공부

목록 보기
117/171

문제 링크 : https://leetcode.com/problems/non-overlapping-intervals/


overlapping하는 intervals을 없애는 가장 최소한의 개수를 반환하는 문제이다
return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

intervals = [[1,2],[2,3],[3,4],[1,3]]이 있으면
[1,3]과 /[1,2][2,3]이 overlapping구간인데

[1,2][2,3]을 없애면 총 두 개를 없애는 것에 비해
[1,3]을 제거하면 하나를 없앨 수 있으므로 [1,3]을 제거랄 한다.
overlapping구간이 있는경우 비교하여 저 작은쪽을 선택히야 한다..

class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        intervals.sort()
        
        res = 0
        prevend = intervals[0][1]
        for s,e in intervals[1:]:
            if s < prevend:
                res+=1
                prevend=min(e, prevend)
            else:
                prevend = e
        return res
profile
성장하고 도전하는 개발자. 프로그래밍 좋아하세요?

0개의 댓글