[프로그래머스 | 해시] 위장 - Python (개선 필요!)

·2022년 1월 13일
0

프로그래머스

목록 보기
8/11
def solution(clothes):
    answer = 0
    
    closet = {}
    
    for name, category in clothes:
        closet[category] = []
    for name, category in clothes:
        closet[category].append(name)
    
    combination = 1
    for items in closet.values():
        combination *= len(items)+1
    
    answer = combination - 1
    return answer
    

고등학교 확통시간에 배운 Combination!
최소 1개 이상의 옷을 입어야 하니, 옷을 다 벗은 경우 1개를 마지막에 빼주면 됨

카테고리별로 리스트를 먼저 할당해야 한다고 생각해서 for문을 두번 돌아 closet dictionary에 리스트 형태로 저장했는데, 더 효율적인 방법이 있을까?

profile
튼튼

0개의 댓글