옷의 타입별 개수를 구해서 조합을 구했다.
두 가지 방식으로 풀이했는 (1) 딕셔너리 사용 (2) set과 list의 count를 사용해서 옷 타입 개수 구하기
1번 방식의 풀이가 더욱 직관적이지만, 2번 방식의 접근이 신선하다.
리스트 타입에 있는 원소들을 set()하여 고유의 원소만 새로운 변수에 담았다.
그 다음 리스트의 count()를 사용해서 각 리스트에 있는 원소의 개수를 구했다.
(1) 딕셔너리 사용
import sys
INPUT = sys.stdin.readline
def get_comb_num_except_zero(cnt_per_type):
""" 착용 가능한 의상의 조합 수를 구한다.
- 각 의상은 종류별로 한 번만 착용 가능하다.
- 모든 의상을 착용하지 않는 경우는 없다.
cnt_per_type: 의상 종류별 개수, 딕셔너리
"""
res = 1
for num in cnt_per_type.values():
res *= num + 1
return res - 1
def solution():
"""
test_case_num: 테스트 케이스 개수
clothes_num: 하나의 테스트 케이스에 주어진 의상의 개수
clothes_type: 의상의 종류
cnt_per_type: 의상의 종류별 개수, 딕셔너리
"""
for _ in range(test_case_num := int(INPUT())):
cnt_per_type = dict()
for _ in range(clothes_num := int(INPUT())):
_, clothes_type = input().split()
cnt_per_type[clothes_type] = cnt_per_type.get(clothes_type, 0) + 1
print(get_comb_num_except_zero(cnt_per_type))
if __name__ == "__main__":
solution()
(2) set과 list의 count를 사용
import sys
# sys.stdin = open("input.txt", "r")
INPUT = sys.stdin.readline
def solution():
for _ in range(test_case_num := int(INPUT())):
types = []
for _ in range(clothes_num := int(INPUT())):
_, clothes_type = INPUT().split()
types.append(clothes_type)
set_types = set(types)
cnt_per_type = map(lambda x: types.count(x) + 1, set_types)
res = 1
for num in cnt_per_type:
res *= num
print(res - 1)
if __name__ == "__main__":
solution()
'코딩 테스트' 카테고리의 다른 글
[백준, 1068] 트리 (0) | 2021.05.28 |
---|---|
[백준, 2636] 치즈 (0) | 2021.05.28 |
[해시][프로그래머스] 베스트앨범 (0) | 2021.05.13 |
[그리디/정렬/힙큐][백준, 11000] 강의실 배정 (0) | 2021.05.12 |
[구현][백준, 2504] 괄호의 값 (0) | 2021.05.11 |