42578 | 위장
Level 2
https://programmers.co.kr/learn/courses/30/lessons/42578


작성 코드
from collections import defaultdict
def solution(clothes):
answer = 0
dict = defaultdict(int)
for cloth, kind in clothes:
dict[kind] += 1
cnts = list(dict.values())
max_value = max(cnts)
final_cnts = 0
for i in range(1, max_value + 1):
temp_cnts = 1
for cnt in cnts:
if cnt >= i:
temp_cnts *= cnt
else:
temp_cnts *= cnt
final_cnts += temp_cnts
return final_cnts
- 한 가지만 입는 경우, 두 가지 입는 경우 ... 두 가지 입는 다면 종류 두가지 뽑고, 경우의 수 곱해주기
이상 코드
from collections import defaultdict
def solution(clothes):
clothes_dict = defaultdict(list)
for sample, category in clothes:
clothes_dict[category].append(sample)
cases = 1
for value in clothes_dict.values():
cases *= len(value) + 1
return cases - 1
- 옷의 category 마다 +1 (안 입는 경우) 해준 후 곱하고, -1 (다 안 입는 경우는 제외 - 최소 한 개 의상은 입음)
반응형
'Algorithm > programmers' 카테고리의 다른 글
| [프로그래머스] 62048 | 멀쩡한 사각형 (0) | 2022.02.08 |
|---|---|
| [프로그래머스] 42579 | 베스트앨범 (0) | 2022.01.18 |
| [프로그래머스] 68935 | 3진법 뒤집기 (0) | 2022.01.11 |
| [프로그래머스] 42577 | 전화번호 목록 (0) | 2022.01.07 |
| [프로그래머스] 42576 | 완주하지 못한 선수 (0) | 2022.01.05 |
댓글