practivceAlgorithm/programmers

[programmers][Python] 2021 kakao blind 메뉴 리뉴얼

findTheValue 2021. 10. 20. 04:50

sets에 조합 출현 갯수 cnt

ans_set에 2번이상이고 최고 많이 나온 comb 입력

answer에 ans_set에 들어간 comb만 추출 후 정렬 반환

 

from collections import defaultdict
from itertools import combinations

def solution(orders, course):
    answer = []
    sets = defaultdict(int)
    for order in orders:
        order = sorted(list(order))
        for num in course:
            for new_comb in combinations(order, num):
                sets[''.join(list(new_comb))] += 1
    ans_set = {num: [] for num in course}
    for comb in sets:
        if sets[comb] < 2:
            continue
        n = len(comb)
        if n in ans_set:
            if not ans_set[n]:
                ans_set[n].append(comb)
            elif sets[ans_set[n][-1]] < sets[comb] :
                ans_set[n].clear()
                ans_set[n].append(comb)
            elif sets[ans_set[n][-1]] == sets[comb]:
                ans_set[n].append(comb)
    for num in ans_set:
        for comb in ans_set[num]:
            answer.append(comb)
    answer.sort()
    return answer