본문 바로가기

ProgramSoliving

프로그래머스 : 구명보트

반응형
import java.util.*;

class Solution {
    public int solution(int[] people, int limit) {
        Arrays.sort(people);
        int front = 0;
        int rear = people.length-1;
        int answer = 0;
        while(front<rear){
         if(people[front] + people[rear]<= limit){
             front++;
             rear--;
         }else{
             rear--;
         }
            answer++;
        }
        
        return answer + (front==rear ? 1 : 0);
    }
}
반응형