문제출처 : https://programmers.co.kr/learn/courses/30/lessons/72411
접근법
코드
- 개선한 코드
from collections import Counter from itertools import combinations def solution(orders, course): answer = [] for course_size in course: # [2,3,4] order_combinations = [] # 가능한 모든 조합 for order in orders: order_combinations += combinations(sorted(order),course_size) # comb -> (comb,count) order_combinations = Counter(order_combinations).most_common() # 가장 많이 나온 조합 찾기 for tup in order_combinations: if( tup[1] == order_combinations[0][1] and tup[1] > 1 ): answer.append( "".join(tup[0]) ) return sorted(answer)
- 이전 코드
from collections import defaultdict from itertools import combinations def find(menu_list,current): global d for i,menu in enumerate(menu_list): comb = current + menu_list[i] d[comb] += 1 find(menu_list[:i]+menu_list[i+1:],comb) def solution(orders, course): answer = [] global d d = defaultdict(int) for order in orders: for i in course: c = list(combinations(order,i)) for t in c: # 튜플 합치기 s = "" for element in t: s += element d["".join(sorted(s))] += 1 for c in course: tmp = defaultdict(set) for key in d: # 코스의 길이 if( len(key) == c ): tmp[d[key]].add("".join(sorted(key))) max_val = 0 for i in tmp: max_val = max(max_val,i) if( max_val > 1 ): for i in tmp[max_val]: answer.append(i) return sorted(answer)