[알고리즘] 해시(Hash) 프로그래머스 2단계 - 위장

minidoo·2020년 9월 5일
0

알고리즘

목록 보기
3/85
post-thumbnail
from collections import Counter

def solution(clothes):
    
    num = [j for i, j in range(len(clothes))]
    result = list(Counter(num).values())
    
    i = 1
    for r in result:
        i = i * (r+1)
    
    return i-1

풀이과정

  1. '의상의 종류'만 필요하기 때문에 따로 배열로 만든다.
  2. 각 종류의 개수를 세기 위해 Counter 클래스를 사용한다.
  3. (종류+1) * (종류+1) ... (종류+1) -1 을 해준다. -1 은 아무것도 입지 않은 경우를 빼준 것이다.

2차 풀이

from collections import Counter

def solution(clothes):
    
    kinds = []
    for i in range(len(clothes)):
        kinds.append(clothes[i][1])
    
    result = 1
    value = list(Counter(kinds).values())
    for j in range(len(value)):
        result *= value[j] + 1
    
    return result-1

0개의 댓글