💻 문제 출처 : 프로그래머스_구명보트
import java.util.Arrays;
class Solution {
public int solution(int[] people, int limit) {
int right = people.length - 1;
int answer = 0;
Arrays.sort(people);
for(int i = 0; i < people.length; i++) {
int left = i;
if(right < left) break;
while(left < right) {
if(people[left] + people[right] <= limit) {
answer++;
people[left] = 0;
people[right] = 0;
right--;
break;
}
right--;
}
}
for(int i : people) {
if (i != 0) answer++;
}
return answer;
}
}
import java.util.Arrays;
class Solution {
public int solution(int[] people, int limit) {
Arrays.sort(people);
int i = 0, j = people.length - 1;
for (; i < j; --j) {
if (people[i] + people[j] <= limit)
++i;
}
return people.length - i;
}
}