공부기록/프로그래머스

[프로그래머스] 귤 고르기

메델 2023. 11. 9. 18:52
import java.util.*;
class Solution {
    public int solution(int k, int[] tangerine) {
        
        int answer = 0;
        int sum = 0;
        
        Map<Integer, Integer> map = new HashMap<>();
        
        for(int x: tangerine){
            map.put(x, map.getOrDefault(x, 0)+1);
        }
        
        List<Integer> list = new ArrayList<>(map.values());
        Collections.sort(list, Collections.reverseOrder());
        
        for(int x: list){
            if(sum + x >= k){
                answer++;
                break;
            }else{
                sum += x;
                answer++;
            }
        }
        
        return answer;
    }
}