구명보트


프로그래머스 [구명보트]

문제보기
Alt text

소스코드

import java.util.Arrays;

class Solution {
 public int solution(int[] people, int limit) {

        Arrays.sort(people);

        int heavy = people.length - 1;
        int light = 0;
        int boatCnt = 0;

        while (light <= heavy) {
            int heavyWeight = people[heavy];
            int lightWeight = people[light];

            if (heavyWeight + lightWeight <= limit) {
                light++;
            }

            heavy--;
            boatCnt++;
        }

        return boatCnt;
    }
}