옷의 타입별 개수를 구해서 조합을 구했다.

두 가지 방식으로 풀이했는 (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()

+ Recent posts