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
'practivceAlgorithm > programmers' 카테고리의 다른 글
[KAKAO 2020][Python] 문자열 압축 (0) | 2021.10.24 |
---|---|
[Kakao2020][Python] 괄호 변환 (0) | 2021.10.24 |
[programmers][Python] 2021 kakao blind 신규아이디 추천 (0) | 2021.10.20 |
[Programmers][Python] 2021 kakaoblind 카드 짝 맞추기 (0) | 2021.10.17 |
[Programmers][Python] 퍼즐조각채우기 (0) | 2021.10.14 |