성우리뷰

구명보트

두원공대88학번뚜뚜 2021. 7. 14. 01:42
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

class Solution {
    public int solution(int[] people, int limit) {
        int answer = 0;
        Arrays.sort(people);
        int left = 0;
        int right = people.length - 1;
        
        while(left <= right) {
            if(left == right) {
                answer++;
                left++;
            }
            
            //가장 무거운애+ 가장 가벼운애가 되면, answer++
            else if(people[left] + people[right] <= limit) { 
                answer++; 
                left++; right--;
            }
            //안되면, 가장 무거운애 혼자 태움
            else{
                answer++;
                right--;
            }
        }
        
        return answer;
    }
}