코드
function solution(people, limit) {
let boat = [];
let num = 1;
people = people.sort((a, b) => b - a);
for (let i = 0; i < people.length; i++) {
if (people[i] > limit / 2) {
boat.push([people[i]]);
} else {
if (boat[boat.length - num][0] + people[i] <= limit) {
boat[boat.length - num].push(people[i]);
num++;
} else {
boat.push([people[i]]);
}
}
}
return boat.length;
}
사람들을 내림차순으로 정렬한 후
limit/2보다 큰 사람들을 boat에 태운다
그리고 마지막 boat에 남은 사람중 가장 무거운 사람이 탈 수 있는지 확인한다
num이라는 변수를 만들어서 마지막 보트에 사람이 탔다면 그 이전 보트와 확인 할 수 있게끔 했다