티스토리 뷰

문제

 

문제 바로가기: [프로그래머스] 메뉴 리뉴얼 - 2021 KAKAO BLIND RECRUITMENT


풀이

#내가 이해를 못한건지, 문제의 애매한 설명 때문인지 문제를 이해하느라 시간이 걸렸었다

#조합

 

#주의 (여기서 너무 헷갈렸다)

1. course가 [2,3,4]일 때, 아래와 같이 모두 구해주면 된다

  - 2개의 조합일 때 가장 많은 조합,

 -  3개의 조합일 때 가장 많은 조합,

 -  4개의 조합일 때 가장 많은 조합,

 - 이 때, 가장 많은 조합은 2개 이상은 있어야 되며, 가장 많은 조합이 여러개 일 때 모두 구한다

 

#풀이

1. orders에서 딕셔너리를 사용해서, course에 있는 수에 맞게 모든 조합에 대해 갯수를 구해줬다

 - course가 [2,3,4]일 때, 2개의 조합, 3개의 조합, 4개의 조합에 대해 아래와 같이 모두 갯수를 구해줬다

 - orders가 ["ABCF", "AC", "BA"]일 때, { AB: 2, AC: 2, AF: 1, ABC: 1, ABF: 1, ABCF: 1 }이 된다

- 이 때, AB와 BA의 조합은 동일하기 때문에, 정렬해서 처리해줬다 

 

2. 위의 주의에서 말한 것처럼, course에 있는 숫자들에 대해 각각 가장 많은 조합을 구해줬다

- course가 2일 때, 2개로 조합된 것 중에서 가장 많은 조합

- course가 3일 때, 3개로 조합된 것 중에서 가장 많은 조합 

- course가 4일 때, 4개로 조합된 것 중에서 가장 많은 조합 

 

3. 정렬하여 답을 도출한다


코드

from itertools import combinations

def solution(orders, course):
    dictionary = {}
    for order in orders:
        for i in course:
            if len(order) >= i:
                combi = list(combinations(sorted(order), i))
                for com in combi:
                    if "".join(com) in dictionary:
                        dictionary["".join(com)] += 1
                    else:
                        dictionary["".join(com)] = 1
    
    answer = []
    for i in course:
        max_value = 1
        result = []
        for key in dictionary:
            if i == len(key):
                if max_value > 1 and max_value == dictionary[key]:
                    result.append(key)
                elif max_value < dictionary[key]:
                    result = [key]
                    max_value = dictionary[key]
                    
        if max_value != 1:
            for res in result:
                answer.append(res)
                
    return sorted(answer)

결과

댓글
Total
Today
Yesterday