본문 바로가기

algorithm/프로그래머스

프로그래머스_구명보트(JAVA)

https://school.programmers.co.kr/learn/courses/30/lessons/42885

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

[JAVA]

import java.util.*;

class Solution {
    public int solution(int[] people, int limit) {
        int answer = 0;
        
        Arrays.sort(people);
        
        int min_index = 0;
        int max_index = people.length - 1;
        
        //가장 무거운 사람과 가장 가벼운 사람을 선택하여 함께 보낼 수 있는 경우 같이 보내고 아닌 경우 무거운 사람 혼자 보낸다.
        while(max_index >= min_index){
            if(people[max_index] + people[min_index] <= limit){
                min_index++;
                max_index--;
                answer++;
            }else{
                max_index--;
                answer++;
            }
        }
        
        return answer;
    }
}