본문 바로가기
코딩테스트/LV. 2

LV.2 :: 귤 고르기

by KUROMI98 2025. 2. 21.

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;
}

'코딩테스트 > LV. 2' 카테고리의 다른 글

LV.2 :: 할인 행사  (0) 2025.02.21
LV.2 :: 괄호 회전하기  (0) 2025.02.21
LV.2 :: 주식 가격  (0) 2024.10.21
LV.2 :: n^2 배열 자르기  (0) 2024.10.21
LV.2 :: 기능개발  (0) 2024.10.21

댓글