알고리즘 문제 풀이/프로그래머스
[프로그래머스] 위장 python
부끄러엇피치
2022. 3. 25. 11:18
728x90
https://programmers.co.kr/learn/courses/30/lessons/42578
출처 : 프로그래머스
코딩테스트 연습 - 위장
programmers.co.kr
def solution(clothes):
answer = 1
clothes_hash = {}
for c_name, c_type in clothes:
if c_type not in clothes_hash: # 처음 해쉬에 넣는 옷 종류면
clothes_hash[c_type] = 2 # 안 입는 경우 까지 더해서 2
else:
clothes_hash[c_type] += 1
for c in clothes_hash.values():
answer *= c
return answer-1
풀이
해시와 경우의 수로 풀이했다.
해시에 넣을 때, 타입의 키가 없는 경우에 2를 더해주는 것에 주의하자.
해당 타입을 고르거나 안 고르거나 (안 입거나) 2가지 경우의 수를 넣어주는 것이다.
최종 경우의 수는 모든 values()의 곱에 -1을 해줘야 한다.
하나도 안 입는 경우는 없기 때문에 -1을 해준다.
경우의 수로 옷 입히기 생각하면 된다.