알고리즘/카카오 기출

[프로그래머스/python] 메뉴 리뉴얼 - 2021 KAKAO BLIND RECRUITMENT

수디sudy 2022. 4. 11. 21:14

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

 

코딩테스트 연습 - 메뉴 리뉴얼

레스토랑을 운영하던 스카피는 코로나19로 인한 불경기를 극복하고자 메뉴를 새로 구성하려고 고민하고 있습니다. 기존에는 단품으로만 제공하던 메뉴를 조합해서 코스요리 형태로 재구성해서

programmers.co.kr

 

 

 

이번 문제는 효율성도 보지 않은 정말 간단한 combination+sorting 문제다

테스트케이스3에서 WX = XW 로 봐야하기 때문에 맨 처음에 orders 리스트 안의 변수 전부를 오름차순 정렬 해주었다.

코드는 너무 간단하니 아래에서 :)

 

 

 

from itertools import combinations
def solution(orders, course):
    answer = []
    
    total = {} #key:코스조합, value:횟수 저장할 딕셔너리
    
    #orders 안의 모든 조합 오름차순 정렬
    temp = []
    for o in orders:
        o = ''.join(sorted(o))	
        temp.append(o)

    orders = temp    
    
    
    #2개 이상 조합별로 몇번 주문되었는지 counting
    for o in orders:
        for n in range(2, len(o)+1):
            o = list(o)
            com = list(combinations(o,n))
            for c in range(0,len(com)):
                tempc = "".join(com[c])
                if tempc in total:
                    total[tempc] += 1
                else:
                    total[tempc] = 1
    

    #2번 이상 주문된 조합 중 가장 많이 나온 조합 저장
    for c in course:
        temp = []
        best = 2
        for t in total:
            if len(t) == c:
                if total[t] == best:
                    temp.append(t)
                elif total[t] > best:
                    best = total[t]
                    temp.clear()
                    temp.append(t)
        answer.extend(temp)
        
    answer.sort()
    return answer