[파이썬] 백준 BOJ 9375번 패션왕 신해빈

강준호·2023년 5월 23일
0

https://www.acmicpc.net/problem/9375

초고

import sys
t = int(sys.stdin.readline())
ans=[]
for _ in range(t):
    clothes = {}
    cnt = 1
    n = int(sys.stdin.readline())
    if n ==0:
        sum =0
    for _ in range(n):
        li = sys.stdin.readline().split()
        category = li[1]
        count = clothes.get(category,0) #키가 없으면 0
        clothes[category] = count+1
    for count in clothes.values():
        cnt *= count
    if len(clothes.items())>1:
        cnt += n
    ans.append(cnt)
for i in ans:
    print(i)

처음에 보고 각 옷 피스들끼리에 곱 + 각각 하나씩 입을 경우를 따로 더하자 라고 생각했었다.

=> 전체 모든 피스를 입는게 아니기 때문에 오답!

2트

import sys

t = int(sys.stdin.readline())
ans=[]
for _ in range(t):
    clothes = {}
    total = 1
    n = int(sys.stdin.readline())
    if n ==0:
        cnt =0
    for _ in range(n):
        li = sys.stdin.readline().split()
        category = li[1]
        count = clothes.get(category,0) #키가 없으면 0
        clothes[category] = count+1

    for item in clothes.values():
        total *= (item+1)
    ans.append(total-1)
for i in ans:
    print(i)

옷을 안입는 경우까지 포함해 한 카테고리의 옷이 n개라면 n+1 을 세줘야한다.

그리고 맨마지막 계산 결과에 알몸일때를 -1 해주는게 현명하다.

시간복잡도

  • O(N)

0개의 댓글