-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseven-dwarfs.js
37 lines (32 loc) · 890 Bytes
/
seven-dwarfs.js
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
/**
* @desc problem : 일곱 난쟁이
* @desc site : Olympiad
* @desc level: 1
*/
/**
* solution
* @param {array} height
*/
function solution(height) {
const fakeDwarfs = [];
const max = 100;
const totalHeight = height.reduce(function (acc, item) {
return acc + item;
});
height.forEach(function (item, index, array) {
const sliceIndex = index + 1;
const compare = array.slice(sliceIndex);
compare.forEach(function (compareItem, compareIndex) {
if (totalHeight - (item + compareItem) === max) {
fakeDwarfs.push(sliceIndex + compareIndex);
fakeDwarfs.push(index);
}
});
});
fakeDwarfs.forEach(function (item) {
height.splice(item, 1);
});
return height;
}
const answer = solution([20, 7, 23, 19, 10, 15, 8, 13, 25]);
console.log(answer);