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

AI·2025년 9월 10일

https://school.programmers.co.kr/learn/courses/30/lessons/42885
2명씩만 탈 수 있기에 몸무게가 제일 적은 사람과 제일 큰 사람을 계속 구해서 두 명 값을 더해서 limit을 안 넘으면 넣기

class Solution {
    public int solution(int[] people, int limit) {
        int ans = 0;
        Arrays.sort(people);
        
        int i = 0;
        int j = people.length-1;

        while(i<=j){
            if(people[i]+people[j] <= limit){
                i++;
            }
            
            j--;
            ans++;
        }
        
        return ans;
    }
}

0개의 댓글