쵼쥬 2021. 11. 23. 22:38


최대 2명이라는 조건을 보지 못해서 잘못된 풀이를 하고 있었다..

 

풀이 방법

정렬해준 다음 최대 2명이라서 가장 작은 사람과 가장 큰 사람을 묶어서 생각했다.

두 사람이 limit를 초과하면 큰사람만 계산해 주었다.

 

내 코드

import java.util.*;

class Solution {
    public int solution(int[] people, int limit) {
        int answer = 0;
        int n = people.length;
        Arrays.sort(people);
        int left = 0;
        int right = n - 1;
        
        while(left <= right){
            if(people[left] + people[right] <= limit){
                right--;
                left++;
            }else
                right--;
            answer++;
        }
        
        return answer;
    }
}