# default dict로 각 수를 세는 딕셔너리 생성
# 겹치는 것을 추가하는 함수를 만들어서 계속 딕셔너리에 추가
# 마지막에 key의 개수별로 가장 높은 것을 list에 추가해주고\
# sort하기.
from collections import defaultdict
from itertools import combinations
def solution(orders, course):
store = defaultdict(int)
addToStore(orders, store)
result = []
for course_num in course :
cur_dict = dict(filter(lambda x : len(x[0]) == course_num and x[1] >= 2 , store.items()))
if cur_dict :
cur_max = max(cur_dict.values())
for key in dict(filter(lambda x: x[1] == cur_max , cur_dict.items())).keys() :
result.append(key)
return sorted(result)
def addToStore (orders,store) :
permus = [combi for order in orders for num in range(2, len(order)+1) for combi in combinations(order,num)]
for k in permus:
store[''.join(sorted(k))] += 1
return
우선, 풀었다는 것에 의의를 두고 싶다. ㅋㅋㅋ...
꽤나 논리적으로 길었다. 물론, 쓰면서도 훨씬 간단하게 만들 수 있다는 생각이 들긴 했지만 우선 푸는데 집중하였다.
import collections
import itertools
def solution(orders, course):
result = []
for course_size in course:
order_combinations = []
for order in orders:
order_combinations += itertools.combinations(sorted(order), course_size)
most_ordered = collections.Counter(order_combinations).most_common()
result += [ k for k, v in most_ordered if v > 1 and v == most_ordered[0][1] ]
return [ ''.join(v) for v in sorted(result) ]
기본적인 논리는 비슷하다.
하지만 두 가지 다른 점이 있고, 그 두 가지 다른 점이 큰 차이를 만들었다.
해당 course의 인자에 해당하는 길이을 기준으로만 combination을 실행하여 list에 모아주고,
그 list에서 가장 많이 나타나는 요소들을 result에 담아준 것이다.