[PS] Leet | 435. 겹치지 않는 구간들

2025. 4. 30. 21:40·CS/PS

문제

Given an array of intervals intervals where intervals[i] = [start_i, end_i], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Note that intervals which only touch at a point are non-overlapping. For example, [1, 2] and [2, 3] are non-overlapping.

예시

Example 1:
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.


Example 2:
Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2
Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.


Example 3:
Input: intervals = [[1,2],[2,3]]
Output: 0
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.

조건

  • 1 <= intervals.length <= 105
  • intervals[i].length == 2
  • -5 * 104 <= starti < endi <= 5 * 104

답

Greedy로 해결할 수 있습니다.

 

최소한의 구간을 제거하여 겹친 부분이 없게 만들어야 한다는 조건을 구간의 개수를 최대한 많이 남기면서 겹친 부분이 없게 만들어야 한다는 조건으로 해석할 수 있습니다.

 

intervals를 end_i의 오름차순으로 정렬한 후 겹치는 부분이 생기는 구간들만 제거한다면, 위와 같은 조건을 만족시킬 수 있습니다.

/**
 * @param {number[][]} intervals
 * @return {number}
 */
var eraseOverlapIntervals = function(intervals) {
    const len = intervals.length;
    let minNum = 0;
    let index = 0;

    if (len === 0) return 0;

    intervals.sort((a,b) => a[1] - b[1]);

    for (let i=1; i<len; ++i) {
        if (intervals[i][0] < intervals[index][1]){
            minNum++;
        } else {
            index = i;
        }
    }

    return minNum;
};

시간 복잡도 : O(nlogn)

공간 복잡도 : O(1)

728x90

'CS > PS' 카테고리의 다른 글

[PS] Leet | 23. 정렬된 K개의 리스트 합치기  (0) 2025.05.09
[PS] Leet | 141. 연결 리스트 순환  (0) 2025.05.03
[PS] Leet | 57. 구간 삽입  (0) 2025.04.26
[PS] Leet | 128. 가장 긴 연속 수  (0) 2025.04.24
[PS] Leet | 207. 과목 스케쥴  (0) 2025.04.19
'CS/PS' 카테고리의 다른 글
  • [PS] Leet | 23. 정렬된 K개의 리스트 합치기
  • [PS] Leet | 141. 연결 리스트 순환
  • [PS] Leet | 57. 구간 삽입
  • [PS] Leet | 128. 가장 긴 연속 수
Rayi
Rayi
  • Rayi
    아카이브
    Rayi
  • 전체
    오늘
    어제
    • 분류 전체보기 (262) N
      • CS (40)
        • ML (3)
        • CV (2)
        • PS (34)
      • Reveiw (17)
        • Paper (17)
        • Github (0)
      • Pytorch (5)
      • Language (58)
        • Python (7)
        • JavaScript (32)
        • TypeScript (16)
        • C++ (3)
      • IDE (12)
      • Git (13)
      • Frontend (71) N
        • React (8)
        • SolidJS (20)
        • CSS (12) N
      • UI (3)
      • Backend (15)
        • DB (17)
        • Node.js (11)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    deploy
    API
    CSS
    PyTorch
    ps
    PRISMA
    ML
    mongo
    Git
    DB
    UI
    postgresql
    SOLID
    figma
    Express
    vscode
    backend
    Three
    js
    GAN
    CS
    react
    ts
    python
    html
    C++
    CV
    frontend
    nodejs
    review
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
Rayi
[PS] Leet | 435. 겹치지 않는 구간들
상단으로

티스토리툴바