Algorithm/프로그래머스

[프로그래머스] 의상 - 파이썬

potato_pizza 2024. 6. 19. 17:54
728x90

의상

https://school.programmers.co.kr/learn/courses/30/lessons/42578

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

문제

 

코드

  • 해시 딕셔너리 구조를 활용한 풀이
  • key, value 부여
  • 최종적인 조합의 수는 안입는 경우까지 포함하여 종류의 개수를 곱하고 최종 값에서 -1
def solution(clothes):
    answer = 1
    dic = {}
    for i in range(len(clothes)):
        value, key = clothes[i][0], clothes[i][1]
        if key in dic:
            dic[key].append(value)
        else:
            dic[key] = [value]

    for i in dic:
        answer *=int(len(dic[i])+1)


    return answer -1

 

  • collections 모듈의 Counter를 활용한 풀이
from collections import Counter

def solution(clothes):
    # 옷의 종류별로 카운트하기 위한 빈 Counter 객체 생성
    count = Counter()

    # clothes 리스트를 반복하며 각 옷의 종류를 카운트
    for item in clothes:
        name, category = item  # 각 항목에서 이름과 종류 추출
        count[category] += 1  # 해당 종류의 카운트를 증가

    # 각 종류의 옷의 개수에 1을 더하여 계산 (해당 종류를 입지 않는 경우 포함)
    answer = 1
    for num in count.values():
        answer *= (num + 1)

    # 모든 종류를 입지 않는 경우는 없으므로 1을 빼줍니다.
    return answer - 1
728x90
반응형