오늘도 어김없이 코테 1문제를 풀었다. 이번에 푼 문제는 2309번 : 일곱 난쟁이 였다. 나는 9명의 난쟁이 중 7명의 키의 합이 100이라는 것에 초점을 맞췄고, 9명의 난쟁이 배열에서 7명을 무작위로 추출하여 키의 합이 100이 되는 난쟁이를 찾고 있었다. 이는 메모리 초과가 발생했다.....ㅋㅎ
그래서 다른 사람의 2309번 풀이 블로그를 봤다. 그랬더니 웬걸 이 사람은 역으로 생각해서 '난쟁이 키의 총합 - 100 = 가짜 난쟁이의 키 합'이라는 사실을 이용해서 풀고 있더라. 대단해.... 나도 앞으로는 좀 더 편한 풀이를 생각해봐야겠다.
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentNumber) => accumulator + currentNumber);
console.log('sum =', sum); //15
let months = ["Jan", "Feb", "Sunday", "Monday"];
let days = months.splice(2); // 인덱스 2부터 배열 변경
console.log(days); // ["Sunday", "Monday"]
let months = ["Jan", "Feb", "Sunday", "Monday"];
let days = months.splice(2, 1);
console.log(months); // ["Jan", "Feb", "Monday"]
console.log(days); // ["Sunday"]
const heights = [20, 34, 8, 19, 48]
for (let i = 0; i < 2; i++) {
let moveheight = input.splice([Math.floor(Math.random() * input.length)], 1)[0];
height.push(moveheight);
} // 20, 8 | 34 19 | 20 48 등등
const arr = input.sort();
//1, 10, 24, 35, 4, 5, 81
const arr = input.map(index).sort((a, b) => a - b);
// 1, 4, 5, 10, 24, 35, 81