구명보트

magicdrill·2025년 1월 28일
0

구명보트

import java.util.Arrays;

class Solution {
    public int solution(int[] people, int limit) {
        int answer = 0, weight = 0;
        int i, left = 0, right = people.length - 1;
        
        //그리디 : 그 상황에서 최선의 방법
        Arrays.sort(people);
        while (left <= right) {
            if (people[left] + people[right] <= limit) {
                left++;
            }
            right--;
            answer++; 
        }
        
        return answer;
    }
}

0개의 댓글