팀장님은 프로젝트 팀원 10명의 기여 점수를 보고 깜짝 놀랐다.
100점으로 딱 맞춰야 하는 프로젝트 총 기여점수가 이상하게 초과되어 있었기 때문이다.
알고 보니, 실수로 점수가 반영된 팀원 두 명이 포함되어 있었던 것이다.
let scores = [14, 20, 7, 8, 13, 12, 17, 5, 9, 11];
"8명 중 합이 100이 되는 조합을 찾아야 하나?" 라고 생각했지만,
총합에서 2명을 제거하는 방식이 더 빠르고 간단했다.
function solution(scores){
const total = scores.reduce((a, b) => a + b, 0);
for(let i = 0; i < scores.length - 1; i++) {
for (let j = i + 1; j < scores.length; j++) {
if (total - scores[i] - scores[j] === 100) {
return scores.filter((_, idx) => idx !== i && idx !== j);
}
}
}
}
let scores = [14, 20, 7, 8, 13, 12, 17, 5, 9, 11];
console.log(solution(scores));