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

강주형·2022년 8월 31일
0

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

2021 KAKAO BLIND RECRUITMENT

from collections import defaultdict
from itertools import combinations

def solution(orders, course):
    answer = []
    for c in course:
        count = defaultdict(int)
        for ordrs in orders:
            for i in list(combinations(sorted(ordrs), c)):
                count[i] += 1
        answer += [k for k,v in count.items() if max(count.values()) == v and v > 1]
    return sorted([''.join(a) for a in answer ])

내장 라이브러리를 많이 알고 있으면 어렵지 않게 풀리는 것 같다.
몇 가지 살펴볼 점이 있다.

  1. 정렬 먼저 하고 조합을 만들어서 개수 세기
  2. 딕셔너리에서 value가 최대값이면서 2 이상인 key를 모두 뽑기

타인 코드

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) ]

Counter 객체를 사용한 것 외에는 로직이 거의 비슷한 것 같다.

profile
Statistics & Data Science

0개의 댓글