[프로그래머스] 체육복 (javascript)

728x90
반응형

 

function solution(n, lost, reserve) {
// 여분의 사람 중 잃어버린 사람 빼준다 
    const res=reserve.filter(r=>!lost.includes(r));
    // 잃어버린 사람들 중에서도 여분의 옷을 가지고 있다면 빼준다 
    const los=lost.filter(l=>!reserve.includes(l));
    
    for(let i=0;i<res.length;i+=1){
        for(let j=0;j<los.length;j+=1){
        	앞이나 뒷 사람에게 줄 수 있다면 지워준다 
            if( los[j]===res[i]-1 ||los[j]===res[i]+1){
                los[j]=-1;
                break;
            }
        }
    }
    // 전체 명 수에서 마지막 까지 체육복 없는 사람 수 빼기
    return n-los.filter(l=>l!==-1).length;
}

 

728x90
반응형
TAGS.

Comments