https://programmers.co.kr/learn/courses/30/lessons/42578
✅ Solution
- defaultdict을 이용하여 옷을 종류별로 구분해준다.
- 종류별로 0개 혹은 1개를 선택할 수 있으므로 각 종류별 길이 + 1을 한 뒤 곱한다.
- headgear : yellow_hat, green_turban / eyewear: blue_sunglasses -> (2+1) * (1 + 1) = 6
- 근데, 문제에서 최소 1개의 의상을 입는다고 했으니 모두 0개를 고르는 한개의 경우를 빼주고 리턴한다.
✅ Code
from collections import defaultdict
def solution(clothes):
answer = 1
allClothes = defaultdict(list)
for cloth in clothes:
name, kind = cloth
allClothes[kind].append(name)
for key in allClothes.keys():
answer *= (len(allClothes[key]) + 1)
return answer - 1
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - 2개 이하로 다른 비트 (Python) (0) | 2022.06.23 |
---|---|
[프로그래머스] Level 2 - 다리를 지나는 트럭 (Python) (0) | 2022.06.22 |
[프로그래머스] Level 2 - 게임 맵 최단거리 (Python) (0) | 2022.06.09 |
[프로그래머스] Level 2 - 배달 (Python) (0) | 2022.06.09 |
[프로그래머스] Level 2 - 괄호 회전하기 (Python) (0) | 2022.06.09 |