메뉴 리뉴얼

메캉·2022년 7월 20일
0

알고리즘 👑

목록 보기
7/11

URL

https://school.programmers.co.kr/learn/courses/30/lessons/72411/solution_groups?language=python3

개선할 점

  1. 데이터 개수를 셀 때 : collection counter
    from collections import Counter
    Counter('hello world') # Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': >1, 'r': 1, 'd': 1})

내 코드

from itertools import combinations

dic = {}
answer=[]

def findMax(dic):
    
    m = [k for k,v in dic.items() if max(dic.values()) == v and v >=2]
    for mm in m:
        answer.append(mm)
    answer.sort()
            

def combi(o, c, dic):
    #o list에서 c길이만큼의 combination 조합을 dic에 담음
    cb = list(combinations(o, c))
    for i in cb:
        i = sorted(i)
        ele = ''.join(i)
        try:
            dic[ele] += 1
        except:
            dic[ele] = 1            
        
def solution(orders, course):
    global answer
    answer = []
    global dic
    for c in course:
        dic = {}
        for o in orders:
            combi(o, c, dic)
        # 여기서 dic 의 최대값을 추출해야 함
        print(dic)
        if dic != {}:
            findMax(dic)
        
    return answer

가이드 코드

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

0개의 댓글