프로그래머스 42885번 구명보트 Java

: ) YOUNG·2024년 3월 1일
1

알고리즘

목록 보기
329/370
post-thumbnail

프로그래머스 42885번
https://school.programmers.co.kr/learn/courses/30/lessons/42885?language=java

문제



생각하기


  • 분류에 그리디 문제라고 되어있는데, 나는 그냥 투 포인터가 생각나서 투 포인터를 적용해서 풀었다.


동작



결과


코드



import java.util.*;

class Solution {

    public int solution(int[] people, int limit) {
        Arrays.sort(people);
        int n = people.length;
        int ans = 0;
        int low = 0;
        int high = n - 1;
        
        while(low <= high) {
            if(people[low] + people[high] <= limit) {
                low++;
            }
            
            high--;
            ans++;
        }
        
        return ans;
    } // End of solution()
} // End of Solution class

0개의 댓글