[LeetCode] 646. Maximum Length of Pair Chain

Chobby·2026년 5월 20일

LeetCode

목록 보기
1079/1083

😎풀이

  1. pairs의 종료 지점을 기준으로 오름차 순 정렬
  2. 가장 종료 지점이 빠른 요소부터 연결 가능한 모든 체인을 연결
  3. 연결된 가장 많은 체인의 수 반환
function findLongestChain(pairs: number[][]): number {
    const sorted = pairs.toSorted((a, b) => {
        if(a[1] !== b[1]) return a[1] - b[1]
        return a[0] - b[0]
    })
    let chained = 1
    let lastRight = sorted[0][1]
    for(let i = 1; i < sorted.length; i++) {
        const [left, right] = sorted[i]
        if(left <= lastRight) continue
        chained++
        lastRight = right
    }
    return chained
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글