코딩테스트/LV. 2

LV.2 :: 귤 고르기

KUROMI98 2025. 2. 21. 01:54

https://school.programmers.co.kr/learn/courses/30/lessons/138476

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

function solution(k, tangerine) {
    const countMap = new Map();

    // 개수 카운팅
    for (const t of tangerine) {
        countMap.set(t, (countMap.get(t) || 0) + 1);
    }

    // 개수를 기준으로 내림차순 정렬
    const sortedCounts = [...countMap.values()].sort((a, b) => b - a);

    let sum = 0;
    let answer = 0;

    for (const count of sortedCounts) {
        sum += count;
        answer++;
        if (sum >= k) break;
    }

    return answer;
}