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

박신희·2022년 4월 22일
0

[풀이] 프로그래머스

목록 보기
17/33
post-thumbnail

❗️ 풀이과정

  • course별로 최소 2명이상이 시킨 코스이면서 최대로 시킨 코스를 골라서 출력하는 것이다.
  • 함수 Combinations 사용 → 각 order에서 나올 수 있는 조합을 구한다.
  • 함수 Counter 사용 → 나올 수 있는 조합의 횟수를 dictionary에 저장 ( key: 메뉴조합, value: 횟수)
  • 조합이 없는 경우도 있기 때문에 if문을 써서 있는 경우만 max_value를 구하도록 설정
  • max_value는 max함수를 쓰지 않고 이미 value 내림차 순으로 정렬을 했기 때문에 첫번째 요소의 value를 max_value로 했음

🤜 풀이 코드

from itertools import combinations
from collections import Counter

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

    for c in course:
        arr=[]
        for order in orders:
            for i in combinations(order,c):
                tmp=''.join(sorted(i))
                arr.append(tmp)        
        candidates_cnt=dict(Counter(arr))
        s_candidates_cnt=sorted(candidates_cnt.items(),key=lambda x:x[1],reverse=True)
        if s_candidates_cnt:
            max_value=s_candidates_cnt[0][1]
        print("max_value",max_value)
        
        for k,v in candidates_cnt.items():
            if v==max_value and v>=2:
                answer.append(k)
    # 사전순으로 (오름차순) 정렬            
    return sorted(answer)
profile
log my moments 'u')/

0개의 댓글