Today
Total
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
관리 메뉴

기억은 짧고 기록은 길다

[프로그래머스/Programmers] 위장 - Python 본문

CodingTest/programmers

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

ukunV 2021. 9. 19. 00:25

Link

 

코딩테스트 연습 - 위장

 

programmers.co.kr

 

Solution

해당 문제는 종류 별로 개수를 파악한 후 (개수 + 1) 값을 곱해주는 것만 파악하면 쉽게 해결할 수 있는 문제였다. 마지막에 -1은 아무것도 착용하지 않은 경우를 빼주는 역할을 한다.

def solution(clothes):
    answer = 1

    d = {}
    for i in range(len(clothes)):
        if clothes[i][1] in list(d.keys()):
            continue

        d[clothes[i][1]] = 1
        for j in range(i + 1, len(clothes)):
            if clothes[i][1] == clothes[j][1]:
                d[clothes[i][1]] += 1

    v = list(d.values())

    for i in range(len(v)):
        answer *= (v[i] + 1)

    return answer - 1
🔑 key point: Dictionary, answer *= (v[i] + 1)

 

Other Solution

해당 풀이를 보고 정말 감탄을 금치 못했다. Counter()와 reduce()의 사용법만 안다면 풀이를 쉽게 이해할 수 있으므로 참고에 Counter()와 reduce()에 대한 자세한 설명이 되어있는 글을 첨부하도록 하겠다.

from collections import Counter
from functools import reduce

def solution(clothes):
    cnt = Counter([kind for name, kind in clothes])
    answer = reduce(lambda x, y: x * (y + 1), cnt.values(), 1) - 1
    return answer
🔑 key point: Counter(), reduce()

첨부

https://excelsior-cjh.tistory.com/94

https://codingpractices.tistory.com/67?category=1026198

Comments