Problem Solving

프로그래머스 위장

onaeonae1 2022. 1. 10. 13:22

문제 설명

- 복장에 대해서, List로 [value, key] 가 주어진다.

 e.g) [["Sunglasses", "headwear"], ["Coat", "BodyWear"], ["AweSomeGlass", "headwear"] 과 같은 식으로 입력 들어옴

    -> { "headwear":["Sunglasses", "headwear"], "BodyWear": "Coat" }

- 이때, 같은 key의 옷은 입을 수 없을때 가능한 모든 옷 조합을 구하는 문제

- 간단하게 풀어보자면, 조합을 하나씩 구성하는 것보다 경우의 수로 접근하는게 편함

 

문제 풀이

- 경우의 수로 접근하는데, 각 옷 카테고리(=key) 별로 존재하는 옷의 수들을 모두 곱한 다음에 -1 로 처리

- 왜냐하면, 아무것도 착용하지 않는 것은 제외해야 하므로

- key 에 따라서 옷들을 가져오는 정리하는 작업은 dict로 처리. {카테고리: 갯수} 로 정리하면 편함

 -> C++에서의 stl map 이나 Java의 HashMap, Python Dict 등으로 구현하면 됨

 

코드

def solution(clothes):
    answer = 1
    clothes_dict = {}
    for cloth in clothes:
        cloth_value, cloth_key = cloth
        clothes_dict[cloth_key] = 1 if clothes_dict.get(cloth_key) == None else clothes_dict.get(cloth_key)+1
    
    for cloth_key, cloth_value in clothes_dict.items():
        answer = answer * (cloth_value+1)    
    
    answer = answer - 1
    
    return answer

 

기타

- 파이썬은 너무 편하다