일곱난쟁이
function solution(candidates) {
let answer = [...candidates], total = 0;
const length = candidates.length -1;
for( let i = 0; i < length; i++ ) {
total += candidates[i];
}
const targetNum = total - 100;
loop: for( let i = length; i > 0; i-- ) {
for ( let j = i - 1; j > 0; j-- ) {
if (candidates[i] + candidates[j] >= targetNum) {
answer.splice(i, 1);
answer.splice(j, 1);
break loop;
}
}
}
return answer;
};
입력예제
const candidates = [20, 7, 23, 19, 10, 15, 25, 8, 13];
console.log(solution(candidates));
출력예제
[ 20, 7, 23, 19, 10, 15, 8 ]