Problem | 체육복
여벌 체육복을 가져온 학생이 체육복을 도난당했을 수 있습니다. 이때 이 학생은 체육복을 하나만 도난당했다고 가정하며, 남은 체육복이 하나이기에 다른 학생에게는 체육복을 빌려줄 수 없습니다.
이 문장을 해석하지 못해서 여러 번 틀렸다.
function solution(n, lost, reserve) {
var answer = 0;
const student=new Array(n+1).fill(true);
let count=0;
for(let x of lost){
const index=reserve.indexOf(x);
if(index!==-1){
const prev=reserve.slice(0,index);
const next=reserve.slice(index+1);
reserve=[...prev,...next]
continue;
}
student[x]=false;
count++;
// false 인 숫자 count
}
reserve.sort((a,b)=>a-b);
for(let x of reserve){
if(x!==1 && !student[x-1]){
student[x-1]=true;
count--;
continue;
}
if(x===n) continue;
if(student[x+1]) continue;
student[x+1]=true;
count--;
}
answer=n-count;
return answer;
}