[프로그래머스][구명보트]-Lv.2

호준·2022년 11월 16일
0

Algorithm

목록 보기
102/111
post-thumbnail

🎊 문제

문제링크

🎊 제한사항

🎊 접근방법

  1. 오름차순정렬을 한다.
  2. 투포인터를 사용하였다.
  3. 작은 값들을 변경하면서 구한다.

🎊 코드

import java.util.*;
class Solution {
    public int solution(int[] people, int limit) {
        int answer = 0;
        Arrays.sort(people);
        int left = 0;
        int right = people.length-1;
        int sum = people[right--];
        while(left <= right){
            if(sum + people[left] <= limit){
                sum += people[left++];
            }else{
                answer++;
                sum = people[right--];
            }
        }
        if(sum <= limit) answer++;
        return answer;
    }
}
profile
도전하지 않는 사람은 실패도 성공도 없다

0개의 댓글