public int solution(int k, int[] tangerine) {
Map<Integer, Integer> sizeMap = new HashMap<>();
for (int size : tangerine) {
sizeMap.put(size, sizeMap.getOrDefault(size, 0) + 1);
}
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> sizeMap.get(b) - sizeMap.get(a));
pq.addAll(sizeMap.keySet());
int answer = 0;
while (!pq.isEmpty() && k > 0) {
int size = pq.poll();
int count = Math.min(k, sizeMap.get(size));
answer++;
k -= count;
}
return answer;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/138476