[프로그래머스] 의상

rhkr9080·2023년 12월 2일
0

프로그래머스

목록 보기
11/19

문제링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42578

💻 문제 풀이 : Python

⭐ 내 풀이
def solution(clothes):
    hash_map = {}
    for clothe, type in clothes:
    	#옷 종류의 가짓 수 저장, 이전에 값이 있었으면 기존 값에 +1
        hash_map[type] = hash_map.get(type, 0) + 1
    print(hash_map)
    answer = 1
    for type in hash_map:
    	#모든 옷 종유에 대해서 안 입는 경우도 있기 때문에 +1을 해줌
        answer *= (hash_map[type] + 1)
    print(hash_map)
    return answer - 1 #아무것도 입지 않은 경우 1를 제외 시켜줌

📌 memo

  • hash에서 get함수
hash_map.get(type, 0)
# None인 경우 0을 리턴한다!
  • hash에서 value를 list로 만들기
def solution(clothes):
    answer = 0
    dic = {}
    for cloth in clothes:
        if dic.get(cloth[1]) == None:
            dic[cloth[1]] = [cloth[0]]
        else:
            dic[cloth[1]].append(cloth[0])
    # print(dic, len(dic))
profile
공부방

0개의 댓글