개발생각11 2024. 3. 23. 00:03
반응형

구명보트 : 탐욕법(Greedy) Lv.2

 

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

 

프로그래머스

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

programmers.co.kr

 

import java.util.Arrays;

class Solution {
    public int solution(int[] people, int limit) {
        int answer = 0;
        
        // TODO Auto-generated method stub
		//int[] people = {10,20,70,80,90};
		//int limit = 100;
		//int boat=0;
		
		int idx = 0;
		Arrays.sort(people);
		
		for(int i= people.length-1; i >= idx; i--) {
			if(people[idx] + people[i] <=limit) {
				idx++;
			}
			
			answer++;
		}
		
		//System.out.println(boat);
        
        return answer;
    }
}

 

반응형