티스토리 뷰

반응형


#. Problem


* The copyright in this matter is in Programmers


스파이들은 매일 다른 옷을 조합하여 입어 자신을 위장합니다.


예를 들어 스파이가 가진 옷이 아래와 같고 오늘 스파이가 동그란 안경, 긴 코트, 파란색 티셔츠를 입었다면 다음날은 청바지를 추가로 입거나 동그란 안경 대신 검정 선글라스를 착용하거나 해야 합니다.


스파이가 가진 의상들이 담긴 2차원 배열 clothes가 주어질 때 서로 다른 옷의 조합의 수를 return 하도록 solution 함수를 작성해주세요.


#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

    - 서로 다른 옷의 조합 수

    - 스파이는 하루에 최소 한 개의 의상

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve

  1. collection.Counter을 사용하여 몇 종류의 옷이 몇 개씩 있는지 확인 {'상의' : '하늘색 반팔'}

  2. 각 종류의 옷 개수에 +1을 하여 입지 않았을 경우도 함께 헤아려 줌(각 옷 종류의 옷 개수를 곱셈 -> 경우의 수 계산)

  3. 최소 한 개의 의상을 입어야 하므로, 맨 마지막에 -1


#. Code

1
2
3
4
5
6
7
8
9
10
import collections
 
def solution(clothes):
    clothes_dic = dict(clothes)
    type = collections.Counter(list(clothes_dic.values()))
    answer = 1
    for coun in list(type.values()) :
        answer = answer * (coun+1)
 
    return answer - 1
cs


#. Other cod

1
2
3
4
5
6
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
cs

  - reduce(function, iterable[, initializer])  

    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5).

    (https://docs.python.org/2/library/functions.html)

반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday