[Programmers] 구명보트 - 탐욕법(Greedy)

동민·2021년 3월 11일
0
import java.util.Arrays;

// 구명보트 - 탐욕법(Greedy)
public class Lifeboat {

	public int solution(int[] people, int limit) {
		int answer = 0, j = 0;
		Arrays.sort(people);
		for (int i = people.length - 1; i >= 0; i--) {
			answer++;
			if (people[i] + people[j] <= limit) {
				j++;
			}
			if (i <= j) {
				break;
			}
		}
		return answer;
	}

}
profile
BE Developer

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN