Link: https://programmers.co.kr/learn/courses/30/lessons/42578
스파이들은 매일 다른 옷을 조합하여 입어 자신을 위장합니다.
예를 들어 스파이가 가진 옷이 아래와 같고 오늘 스파이가 동그란 안경, 긴 코트, 파란색 티셔츠를 입었다면 다음날은 청바지를 추가로 입거나 동그란 안경 대신 검정 선글라스를 착용하거나 해야 합니다.
종류 / 이름
얼굴 / 동그란 안경, 검정 선글라스
상의 / 파란색 티셔츠
하의 / 청바지
겉옷 / 긴 코트
스파이가 가진 의상들이 담긴 2차원 배열 clothes가 주어질 때 서로 다른 옷의 조합의 수를 return 하도록 solution 함수를 작성해주세요.
clothes return
- [["yellowhat", "headgear"], ["bluesunglasses", "eyewear"], ["green_turban", "headgear"]] 5
- [["crowmask", "face"], ["bluesunglasses", "face"], ["smoky_makeup", "face"]] 3
def solution(clothes):
c_dict = {}
for x in clothes:
c_dict[x[1]] = []
for x in clothes:
c_dict[x[1]].append(x[0])
v_list = []
for values in c_dict.values():
v_list.append(len(values)+1)
answer = 1
for x in v_list:
answer *= x
answer -= 1
return answer
def solution(clothes):
clothes_type = {}
for c, t in clothes:
if t not in clothes_type:
clothes_type[t] = 2
else:
clothes_type[t] += 1
cnt = 1
for num in clothes_type.values():
cnt *= num
return cnt - 1
큰 차이점은 없지만 더 간결하고 반복문의 횟수가 적다.