📌문제
https://programmers.co.kr/learn/courses/30/lessons/42578
🎖️난이도
Level 2
✔️풀이
# sol1
def solution(clothes):
sorted_list = sorted(clothes, key = lambda x : x[1])
dic = {}
category = ''
for item in sorted_list:
if item[1] == category:
dic[category].append(item[0]) # 같은 종류면 그냥 뒤에 이어서 추가만
continue
category = item[1]
dic[category] = [] # 다를때만 새로운 종류 발견했을 때이므로, 이때만 새롭게 빈 배열로 초기화!
dic[category].append(item[0])
answer = 1
for key in dic.keys():
answer = answer * (1+len(dic[key]))
answer = answer -1 # 적어도 1개는 입어야 하므로, 모두 다 안 입은 경우 빼기
return answer
# sol2) Counter, reduce()
def solution(clothes):
from collections import Counter
from functools import reduce
cnt = Counter([kind for name, kind in clothes])
answer = reduce(lambda x, y: x*(y+1), cnt.values(), 1) - 1
return answer
'[ 알고리즘 ] > Hash' 카테고리의 다른 글
[프로그래머스] 전화번호 목록 (0) | 2022.06.29 |
---|---|
[프로그래머스] 완주하지 못한 선수 (0) | 2022.06.29 |