Skip to content

[4주차] 백제완 #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 6, 2024
Prev Previous commit
백제완: [PG] 42885 구명보트_241004
  • Loading branch information
Jewan1120 committed Oct 4, 2024
commit 0da6646b822eaddee13fb03c7b13a20add985b1c
19 changes: 19 additions & 0 deletions Programmers/Level2/JW_42885.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Arrays;
class Solution {
public int solution(int[] people, int limit) {
int answer = 0;
Arrays.sort(people); // 투 포인터를 위한 정렬
// 그리디한 방식으로 몸무게가 작은 사람 + 큰 사람을
// 한 보트에 태울 수 있는지 투 포인터로 구현
int l = 0, r = people.length - 1;
while (l <= r) {
// 작은 사람도 같이 태울 수 있는 지 확인
if (people[l] + people[r] <= limit) {
l++;
}
r--;
answer++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

따로 else문 필요 없이 한번에 증가도 가능하군요...!!
저는 2명 타는 경우랑 1명 타는 경우 이렇게 나눠서 생각 하다보니 else문으로 나눠줬었는데, 불필요한 구현 없이 깔끔한 코드 배워갑니다!👍

}
return answer;
}
}