✏️

구명보트

Question

Test Case 1

Shell
복사

Test Case 2

Shell
복사

Solve

아래 코드에서 배열 제거를 하지 않고 확인한 인덱스만 저장해주기로 했음
function solution(people, limit) { people.sort((a, b) => a - b); let count = 0; let start = 0, end = people.length - 1; while (start <= end) { if (people[start] <= limit - people[end]) { start++; } count++; end--; } return count; }
JavaScript
복사
실행시간 : 15.07ms
효율성 테스트 실패,
배열에 값 빼고 slice 하면서 시간 소요되는 듯
function solution(people, limit) { people.sort((a, b) => a - b); let count = 0; let idx = 0; while (people.length > 0) { let weight = people.pop(); if (people[0] <= limit - weight) { people = people.slice(1); } count++; } return count; }
JavaScript
복사
실행시간 : ms