곰퓨타의 SW 이야기

[프로그래머스 level2 메뉴 리뉴얼] itertools, collections 사용하기 본문

TIL/프로그래머스

[프로그래머스 level2 메뉴 리뉴얼] itertools, collections 사용하기

곰퓨타 2021. 6. 11. 22:08

해결해야하는 문제는 다음과 같았다.

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

 

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

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

programmers.co.kr

 

 

 

이 문제에서 주의할 점은 오름차순으로 정렬하는 과정이 있으면 좋다는 점과,

해당 course에 대해 2번 이상 주문이 들어와있다는 것을 생각해야 한다는 점을 유의하면서 문제를 해결하면 된다.

 

이러한 아이디어로 작성한 답은 다음과 같다.

from itertools import combinations
from collections import Counter

def solution(orders, course):
    answer = []
    for cnt in course:
        temp = []
        for order in orders:
            if len(order) >= cnt :
                order = ''.join(sorted(list(order)))
                combi = list(combinations(order,cnt))
                temp += combi
                
        c = Counter(temp)
        temp_cnt = c.most_common()
        if len(temp_cnt)>0 and temp_cnt[0][1]>1:
            max_cnt = temp_cnt[0][1]
            answer.append(''.join(temp_cnt[0][0]))
        
        for i in range(1,len(temp_cnt)):
            if temp_cnt[i][1] == max_cnt:
                answer.append(''.join(temp_cnt[i][0]))
            else :
                break
                
        answer.sort()
        
    return answer
Comments