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

allnight5·2023년 4월 10일
0

프로그래머스

목록 보기
60/73

링크

import java.util.Arrays;
class Solution {
    public int solution(int[] people, int limit) {
        int answer = 0;
        Arrays.sort(people);  
        if(people[0]+people[1]>limit){
            return people.length;
        } 
        int first =0;
        int last = people.length-1;
        while(first<=last){
            if(people[first]+people[last] <= limit){
                answer+= 1;
                first +=1;
                last -=1;      
            }else{
                answer+=1;
                last -=1;     
            }
        }
        return answer;
    }
}
profile
공부기록하기

0개의 댓글