문제이해
각 리스트의 맨마지막이 카테고리이름이고,
각카테고리의 종류에서는 하나밖에 착용할 수 없다.
전부 착용안할 수는 없고 최소 모든 카테고리 포함해서 하나라도 착용해야함
def solution(clothes):
ans = 1
hash = {}
lst = []
for i in clothes:
for j in range(len(i)-1):
if i[-1] in hash:
hash[i[-1]] += 1
else:
hash[i[-1]] = 1
#return hash {'headgear': 2, 'eyewear': 1}
# 각 카테고리의 개수를 센다
for i in hash:
lst.append(hash[i]+1)
# return lst
# 아무것도 착용안한경우 까지 카테고리에 하나 추가해야하므로 +1해서 빈리스트에 넣기
for i in lst:
ans *= i
return ans-1
# 리스트의 모든 수를 다 곱하고 아무것도 착용안한 경우는 없으므로 최종답에서 -1해주기
clothes = [["yellow_hat", "headgear"],
["blue_sunglasses", "eyewear"],
["green_turban", "headgear"]]
print(solution(clothes))
<comment>
1.경우의 수를 구하는 사고방식을 생각할 수 있다면 쉽게 풀었을 문제였다.
ex) 얼굴 : 선글라스, 노란안경, 아무것도 착용안함 상의: 티셔츠 , 아무것도 착용안함 ==> 경우의수 : 3 * 2 - 1 =5
def solution(clothes):
graph = {}
for i in clothes:
if i[1] not in graph:
graph[i[1]] = 1
else:
graph[i[1]] += 1
answer = 1
for i in graph.items() :
answer *= i[1]+1 #answer 에다가 그의상종류를 아무것도 안하는 경우 1개를 추가해서 다곱하기
return answer-1 # 최소 한가지의 의상은 착용해야하므로 모두 안하는 경우 1개제외
clothes = [["crow_mask", "face"], ["blue_sunglasses", "face"], ["smoky_makeup", "face"]]
print(solution(clothes))
'스터디 > 알고리즘' 카테고리의 다른 글
[2659] 십자카드 문제 (0) | 2024.01.13 |
---|---|
[11725] 트리의 부모 찾기 (1) | 2024.01.13 |
[2178] 미로 탐색 (1) | 2024.01.09 |
전화번호 목록 (2) | 2024.01.09 |
폰켓몬 (2) | 2024.01.08 |