

먼저 배열을 정렬한 후 리스트로 바꾼다.
리스트가 0 또는 1이 될 때까지 아래를 반복한다. (min, max 값을 분리하려면 리스트에 최소 2개의 요소가 필요하기 때문)
리스트 길이가 1이라면 한 사람은 남은 것이기 때문에 리스트의 길이를 더해준다.
import java.util.*;
import java.util.stream.*;
class Solution {
public int solution(int[] people, int limit) {
int answer = 0;
// 먼저 배열을 정렬한 후 리스트로 바꾼다.
Arrays.sort(people);
List<Integer> peoples = Arrays.stream(people)
.boxed()
.collect(Collectors.toList());
// 리스트의 크기가 1 또는 0이 될 때까지 아래를 반복한다.
while (peoples.size() > 1) {
int last_index = peoples.size()-1;
// 0번째 인덱스의 값을 min에 넣는다.
int min = peoples.get(0);
// 마지막 인덱스의 값을 max에 넣는다.
int max = peoples.get(last_index);
// 마지막 인덱스의 값을 삭제한다.
peoples.remove(last_index);
// min+max가 limit을 초과하지 않는다면 0번째 인덱스의 값도 삭제한다.
int sum = min + max;
if (sum <= limit) {
peoples.remove(0);
}
// ans에 1을 더한다.
answer += 1;
}
// 리스트 길이가 1이라면 한 사람은 남은 것이기 때문에 리스트의 길이를 더해준다.
return answer + peoples.size();
}
}
