[Python] 프로그래머스 - Level2 - 위장

강주형·2022년 9월 1일
0

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

해시

from collections import defaultdict

def solution(clothes):
    count = defaultdict(int)
    answer = 1
    for c in clothes:
        count[c[1]] += 1
    for c in count.values():
        answer *= (c+1)
    return answer-1

각 의상종류에 +1 한 것을 모두 곱하고 1을 뺀 것이 정답
-> 적어도 하나의 의상은 입어야 하기 때문


타인 코드
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

아이디어는 비슷한데, Counter와 reduce를 사용함

reduce(집계 함수, 순회 가능한 데이터, 초기값)
profile
Statistics & Data Science

0개의 댓글