[Algo] Programmers level2 위장(Hash)

heeeeeeeee·2025년 5월 2일

Algorithm

목록 보기
14/14

from collections import defaultdict
from itertools import combinations
def solution(clothes):
    answer = 0
    clothes_dict = defaultdict(int)
    
    for cloth in clothes:
        clothes_dict[cloth[1]] += 1
    kinds = list(clothes_dict.keys())
    
    for k in range(1, len(kinds) + 1):
        kind = list(combinations(kinds, k))
        
        for n in kind:
            cnt = 1
            for m in n:
                cnt *= clothes_dict[m]
            answer += cnt
                
    return answer
  • 시간 초과
from collections import defaultdict

def solution(clothes):
    answer = 1
    clothes_dict = defaultdict(int)
    
    for cloth in clothes:
        clothes_dict[cloth[1]] += 1
    
    # 각 종류의 옷을 하나씩 고름(선택하지 않는 경우를 위해 + 1)
    for c in clothes_dict:
        answer *= (clothes_dict[c] + 1)
        
    # 아무것도 안입는 경우 1가지 제외 
    return answer-1

0개의 댓글