ror_coding

[Programmers] 귤 고르기 - 138476 본문

Algorithm/Python

[Programmers] 귤 고르기 - 138476

ro_rdil_31 2024. 10. 24. 12:29
728x90

Counter 에서 값들을 기준으로 정렬하는 코드로 .most_common() 이 있다!

 

Question

 

한 상자에 담으려는 귤의 개수 k와 귤의 크기를 담은 배열 tangerine이 매개변수로 주어집니다. 귤 k개를 고를 때 크기가 서로 다른 종류의 수의 최솟값을 return 하도록 solution 함수를 작성해주세요.

 

 

Code

 

from collections import Counter
def solution(k, tangerine):
    cnt = 0
    a = Counter(tangerine)
    
    for item, count in a.most_common():
        k -= count
        cnt += 1
        if k <= 0 : break
    
    
    return cnt

 

now me

On my github

 

728x90