Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d2b9fd3

Browse files
committed
solution 구명보트
1 parent 4b87ea3 commit d2b9fd3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎level-2/구명보트.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,27 @@ function solution(people, limit) {
4040
}
4141

4242
return count;
43+
}
44+
45+
// 정답 3 - iHoHyeon
46+
function solution(people, limit) {
47+
people.sort((a, b) => a - b); // 오름차순 정렬
48+
49+
let cnt = 0; // 구명보트 개수
50+
51+
let front = 0; // 가장 몸무게가 작은 사람의 index
52+
let last = people.length; // 가장 몸무게가 큰 사람의 index
53+
54+
while (last > front) {
55+
cnt++;
56+
57+
const now = people.pop();
58+
last--;
59+
60+
if (now + people[front] <= limit) {
61+
front++; // shift() 연산의 비효율로 인해 front 사용
62+
}
63+
}
64+
65+
return cnt;
4366
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /