본문 바로가기
알고리즘/Java

프로그래머스 코딩테스트 Level 2. 구명보트 with Java

by yhyuk 2024. 9. 20.
728x90
반응형

문제

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

 

프로그래머스

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

programmers.co.kr

 

풀이

import java.util.*;

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

1. 구명보트 최대 2명, 배열 정렬 후 가장 적은 몸무게 + 가장 큰 몸무게사람 비교하여 경우의 수 계산

2. 배열 오름차순 정렬

3. 정렬된 배열 temp 보다 크거나 작을때까지 반복문

 

https://github.com/yhyuk/h-algorithm/tree/master/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/2/42885.%E2%80%85%EA%B5%AC%EB%AA%85%EB%B3%B4%ED%8A%B8

 

728x90
반응형

댓글