LV 2: 귤 고르기

ewillwin·2023년 8월 17일
0

문제 링크

LV 2: 귤 고르기


구현 방식

  • "크기":"개수" 형식으로 count 딕셔너리를 완성
  • count.items()를 list로 변환한 후, 내림차순 정렬
  • count를 순회하는 과정에서 k가 0이하가 될때까지 넘어가는 index+1이 구하고자하는 정답이 된다

코드

from collections import defaultdict

def solution(k, tangerine):
    
    count = defaultdict(int)
    for i in range(len(tangerine)):
        count[tangerine[i]] += 1
    
    count = list(count.items())
    count.sort(key=lambda x: x[1], reverse=True)
    for i in range(len(count)):
        k -= count[i][1]
        if k <= 0: break
        
    return i+1
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글