프로그래머스 LV2 - 의상
내 풀이
def solution(clothes):
cloth_dict = {}
result = 1
for cloth in clothes:
name, type = cloth
cloth_dict[type] = []
for cloth in clothes:
name, type = cloth
cloth_dict[type].append(name)
if len(cloth_dict.keys()) == 1:
return len(cloth_dict[list(cloth_dict.keys())[0]])
for key in cloth_dict.keys():
result *= len(cloth_dict[key])+1
return result - 1
다른 풀이
def solution(clothes):
clothes_type = {}
for c, t in clothes:
if t not in clothes_type:
clothes_type[t] = 2
else:
clothes_type[t] += 1
cnt = 1
for num in clothes_type.values():
cnt *= num
return cnt - 1
풀이 해석
- 애초에 리스트에 추가하는 형식이 아닌 수를 지정하여 더해가는 방식으로 품
- values를 곱해가며 최종적으론 -1(모두 없는 경우)를 해준다
배울 점
- 애초에 dict의 values를 수로 지정한 사고
댓글남기기