296. 메뉴 리뉴얼

아현·2021년 9월 8일
0

Algorithm

목록 보기
310/400



1. Python



from itertools import combinations
import collections
def solution(orders, course):
    
    #각 손님들이 주문할 때 가장 많이 함께 주문한 단품메뉴
    #코스요리 메뉴는 최소 2가지 이상의 단품메뉴
    #최소 2명 이상의 손님으로부터 주문된 단품메뉴 조합에 대해서만 코스요리 메뉴 후보에 포함
    
    #가능한 조합 전부 카운트해서 2이상인 것 course에 있는 수마다
    #오름차순 정렬
    answer = []
    for k in course:
        candidates = []
        for order in orders:
            for li in combinations(order, k):
                res = ''.join(sorted(li))
                candidates.append(res)
        sorted_candidates = collections.Counter(candidates).most_common()
        
        for k, v in sorted_candidates:
            if v > 1 and v == sorted_candidates[0][1]:
                answer.append(k)
    return sorted(answer)

profile
Studying Computer Science

0개의 댓글