[프로그래머스] Lv2 - 위장 (Python)

김멉덥·2023년 7월 1일
0

알고리즘 공부

목록 보기
12/171
post-thumbnail

문제

프로그래머스 코딩테스트 고득점 Kit - 해시

이전 게시글 참고

👉 https://velog.io/@ejung803/프로그래머스-Lv2-위장


코드 구현

정답 코드

def solution(clothes):
    answer = 1

    cloths_comb = dict()

    for i in clothes:
        clothe = i[0]       # 옷 이름
        clothe_type = i[1]  # 옷의 종류
        if(clothe_type in cloths_comb):     # 딕셔너리에 이미 있는 종류라면
            cloths_comb[clothe_type].append(clothe)     # 해당 종류 value list에 추가
        else:   # 없는 새로운 종류라면
            cloths_comb[clothe_type] = [clothe]         # 새로운 value list 생성

    for clothe_type in cloths_comb.keys():      # 종류들만 꺼내봤을 때
        answer *= len(cloths_comb[clothe_type]) + 1     # 종류 안에 있는 value(옷) 개수 + 안입기(1)

    answer -= 1     # 모든 종류를 안입는 경우는 빼기
    print(answer)
    return answer

풀이

  • 해맨 점 = dict() 구현을 안하고 쌩으로 하려했어서 시간이 더 걸렸다.
  • 해시를 위해서는 파이썬에서는 딕셔너리를 구현해야 한다.

profile
데굴데굴 뚝딱뚝딱 개발기록

0개의 댓글