귤 고르기

최민수·2023년 2월 27일
0

알고리즘

목록 보기
20/94
from collections import Counter

def solution(k, tangerine):
    answer = 0
    
    infos = Counter(tangerine)
    infos = dict(sorted(infos.items(), key=lambda x:-x[1]))

    for key, val in infos.items():
        k -= val
        answer += 1

        if k <= 0:
            break
    
    return answer
  • 가장 먼저 defaultdict 이 떠올랐다.
    • 좋은 아이디어지만, 그 전에 Counter로 원소 별 개수를 간단히 세주는 작업이 필요했다.

출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges

profile
CS, 개발 공부기록 🌱

0개의 댓글