1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| function solution(n, lost, reserve) { var answer = 0;
let arr = []; for (let i = 0; i < n; i++) { arr[i] = 1; }
for (let i = 0; i < reserve.length; i++) { arr[reserve[i] - 1]++; }
for (let i = 0; i < lost.length; i++) { arr[lost[i] - 1]--; }
for (let i = 0; i < arr.length; i++) { if (arr[i] == 0) { if (arr[i - 1] == 2) { arr[i - 1]--; arr[i]++; continue; } else if (arr[i + 1] == 2) { arr[i + 1]--; arr[i]++; continue; } } }
const result = arr.filter((value, index) => { return value != 0; });
return result.length; } solution(5, [2, 4], [1, 3, 5]);
|