[프로그래머스] 메뉴 리뉴얼

JinUk Lee·2023년 2월 10일
0

프로그래머스

목록 보기
14/47

https://school.programmers.co.kr/learn/courses/30/lessons/72411

첫번째 풀이


from itertools import combinations

def solution(orders, course):
    answer = []
    menu = []

    for o in orders:
        menu += list(o)
    menu = list(set(menu))
    menu.sort()
    for num in course:

        com = list(combinations(menu,num))
        max_count = 0
        temt = []
        for c in com:
            count = 0
            for o in orders:
                check = True
                for i in range(num):
                    if c[i] not in o:
                        check = False
                if check:
                    count+=1

            if count>=2:
                if count >= max_count:
                    max_count = count
                    temt.append((c,count))

        temt.sort(key=lambda x:(-x[1]))

        for t in temt:
            if t[1]==temt[0][1]:
                answer.append(''.join(t[0]))
    answer.sort()
    print(answer)
    return answer

첫번째 풀이는 조합을 구해서 카운트를 세는 방식으로 답을 구했다.

반복문을 많이 사용했지만 숫자가 작아서 충분할것이라고 생각했는데, 일부 TC에서 시간초과가 발생했다.

profile
개발자 지망생

0개의 댓글