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

Gaanii·2025년 5월 1일

Problem Solving

목록 보기
185/210
post-thumbnail

아래 프로그래머스 로고를 클릭하면 해당 문제로 이동합니다 😀

프로그래머스로고



풀이과정


tangerine의 크기를 딕셔너리로 빈도수(count)를 계산한다.
많이 나온 크기부터 우선 선택해서 k개가 될 때 까지 더하고, 선택한 크기의 개수가 정답이다 !


코드


1. Python

from collections import Counter
def solution(k, tangerine):
    counter = Counter(tangerine)
    print(counter.values())
    cnts = sorted(counter.values(), reverse = True)
    
    total = 0
    result = 0
    for cnt in cnts:
        total += cnt
        result += 1
        if total >= k:
            break
    return result

2. JS

function solution(k, tangerine) {
    const counter = {};
    for(const size of tangerine){
        counter[size] = (counter[size] || 0) + 1;
    }
    
    const cnts = Object.values(counter).sort((a, b) => b - a);
    let total = 0, result = 0;
    for(const cnt of cnts){
        total += cnt;
        result++;
        if(total >= k) break;
    }
    return result;
}


결과


0개의 댓글