
아래 프로그래머스 로고를 클릭하면 해당 문제로 이동합니다 😀
tangerine의 크기를 딕셔너리로 빈도수(count)를 계산한다.
많이 나온 크기부터 우선 선택해서 k개가 될 때 까지 더하고, 선택한 크기의 개수가 정답이다 !
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
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;
}
