240216 귤 고르기

Jongleee·2024년 2월 16일
0

TIL

목록 보기
497/576
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

0개의 댓글