문제
풀이 1
function solution(n, lost, reserve) {
lost.sort((a,b) => b-a)
reserve.sort((a,b) => b-a)
lost = lost.filter(v => {
let check = reserve.indexOf(v)
if (check === -1) return check
else reserve.splice(check,1)
})
return n - lost.filter(a => {
const b = reserve.find(r => Math.abs(r-a) === 1)
console.log(b)
if(!b) return true
reserve = reserve.filter(r => r !== b)
}).length
}
풀이 2
function solution(n, lost, reserve) {
const students = {};
let answer = 0;
for(let i = 1; i <= n; i++){
students[i] = 1;
}
lost.forEach(number => students[number] -= 1);
reserve.forEach(number => students[number] += 1);
for(let i = 1; i <= n; i++){
if(students[i] === 2 && students[i-1] === 0){
students[i-1]++;
students[i]--;
} else if(students[i] === 2 && students[i+1] === 0){
students[i+1]++;
students[i]--;
}
}
for(let key in students){
if(students[key] >= 1){
answer++;
}
}
return answer;
}