[LeetCode] 1893. Check if All the Integers in a Range Are Covered

Chobby·2일 전
1

LeetCode

목록 보기
626/650

😎풀이

  1. 시작 범위를 기준으로 오름차 순 정렬
  2. left부터 right까지 순회
    2-1. 시작 범위가 현재 수보다 늦게 있다면, 생략
    2-2. 종료 범위가 현재 수보다 먼저 있다면, 생략
    2-3. 범위 내에 수가 존재하지 않는다면,false반환
    2-4. 범위 내에 수가 존재한다면, 다음 수 검사
  3. 모든 수가 범위 내에 속한다면, true 반환환
function isCovered(ranges: number[][], left: number, right: number): boolean {
    const sortedRange = ranges.toSorted((a, b) => a[0] - b[0])
    for(let i = left; i <= right; i++) {
        let isValid = false
        for(const [start, end] of sortedRange) {
            if(start > i) continue
            if(end < i) continue
            isValid = true
            break
        }
        if(!isValid) return false
    }
    return true
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글