๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/42885?language=java 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr


1. ๋ฌธ์ œ

 

2. ํ’€์ด

 

3. ์ฝ”๋“œ

import java.io.*;
import java.util.*;

class Solution {
    public int solution(int[] people, int limit) {
        int ans = 0;
        int left = 0;
        int right = people.length-1;
        
        //๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
        PriorityQueue<Integer> pq = new PriorityQueue<> ((o1,o2)->o2-o1);
        for (int i : people) {
            pq.offer(i);
        }
        Integer[] pp = new Integer[people.length];
        int i=0;
        while (!pq.isEmpty()) {
            pp[i++] = pq.poll();
        }
        
        while (left <= right) {
            if (left == right) {
                ans++;
                break;
            }
            
            int sum = pp[left] + pp[right];
            if (sum > limit) {
                left++;
            }
            else {
                left++;
                right--;
            }
            ans++;
        }
        
        return ans;
    }
}

4. ์‚ฌ๋‹ด