[오늘부터 알고리즘] #1-3그리디 알고리즘(Greedy Algorithm) - 구명보트

ma·6일 전
post-thumbnail

💻 오늘의 문제

풀이 과정

import java.util.*;

class Solution {
    public int solution(int[] people, int limit) {
        Arrays.sort(people);
        
        int left = 0; // 가장 가벼운 사람
        int right = people.length - 1; // 가장 무거운 사람
        int answer = 0;
        
        while(left <= right) {
            if(people[left] + people[right] <= limit) {
                left++;
            }
            right--;
            answer++;
        }
        
        
        return answer;
    }
}
  • 문제 핵심: 가장 무거운 사람 기준으로 생각하기.
  • 투포인터를 이용해 턴 마다 가장 무거운 사람을 무조건 태워 보내야 한다는 개념으로 코드 설계
profile
내가 공부하기 위해

0개의 댓글