파이썬 알고리즘-125 (프로그래머스) 메뉴 리뉴얼

jiffydev·2021년 3월 24일
0

Algorithm

목록 보기
132/134

코드

from itertools import combinations

def solution(orders, course):
    answer = []
    res=[]
    for o in orders:
        for l in course:
            o=''.join(sorted(o))
            res+=list(combinations(o,l))
    for l in course:
        res_by_l=[i for i in res if len(i)==l and res.count(i)>=2]
        if res_by_l:
            max_m=max([res_by_l.count(j) for j in res_by_l])
            for m in res_by_l:
                if res_by_l.count(m)==max_m and ''.join(m) not in answer:
                    answer.append(''.join(m))
    return sorted(answer)

다른 사람의 풀이

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) ]
profile
잘 & 열심히 살고싶은 개발자

0개의 댓글