import java.util.Arrays;
public class PRO_42885 {
public static int solution(int[] people, int limit) {
Arrays.sort(people);
int answer = 0;
int min = 0;
int max = people.length - 1;
while (min <= max) {
if (people[min] + people[max] <= limit) ++min;
--max;
++answer;
}
return answer;
}
public static void main(String[] args) {
int[] people = {70, 50, 80, 50};
int limit = 100;
System.out.println(solution(people, limit));
}
}
몸무게 배열을 오름차순으로 정렬한다. 최소값과 최대값을 더한 값이 한계값보다 작거나 같다면 최소값을 증가시켜주고 최대값을 감소하면서 동시에 answer를 증가시킨다. 이는 2명이서 보트 한 개를 이용하는 것과 같다. 만약 조건문을 만족하지 않는다면 최대 몸무게의 사람이 혼자서 보트를 사용한다는 뜻이므로, 최대값만 감소시켜주고 answer를 증가시켜준다.