[Algorithm] 29 week(8.01 ~ 8.07) 3/3

Dev_min·2022년 8월 3일
0

algorithm

목록 보기
92/157

프로그래머스 체육복

function solution(n, lost, reserve) {
    const hasClothes = Array(n).fill(1);
    
    lost.map((lost) => hasClothes[lost-1] = 0);
    
    reserve.map(reserve => hasClothes[reserve-1] += 1);

    for(let i = 0; i < n; i++){
        if(hasClothes[i] === 0 && hasClothes[i-1] ===2){
            hasClothes[i] = 1;
            hasClothes[i-1] = 1;
        }

        if(hasClothes[i] === 0 && hasClothes[i+1] === 2){
            hasClothes[i] = 1;
            hasClothes[i+1] = 1;
        }
    }
    
    return hasClothes.filter(c => c > 0).length;
}
profile
TIL record

0개의 댓글