Problem Solving
[프로그래머스] 위장 (Python3)
흑개1
2021. 10. 21. 15:21
https://programmers.co.kr/learn/courses/30/lessons/42578
from collections import defaultdict
def solution(clothes):
answer = 1
clothing=defaultdict(int)
for name, kind in clothes:
clothing[kind]+=1
for kind in clothing:
answer*=(clothing[kind]+1);
return answer-1;
처음엔 옷 종류 조합을 구해서 각 옷 종류 별 수를 곱해주려다가, 조합을 사용하니 1번 테케에서 시간초과가 발생했다.
그래서 조합을 사용하지 않고, (옷 종류 별 옷 수+1) 를 옷 종류 별로 다 곱해준다. +1 해준것은 해당 옷 종류를 입지 않을 경우다. 다 곱해준 경우의 수에서 다 입지 않을 1가지 경우의 수를 빼면 시간초과가 나지 않고 답이 잘 나온다.