문제
https://programmers.co.kr/learn/courses/30/lessons/72411
내 풀이
orders를 돌면서 order 안에서 c개의 조합으로 만들 수 있는 모든 경우를 combinations를 사용해서 구하였다.
course를 돌 때마다 딕셔너리를 만들어서 조합이 몇 개 나왔는지 카운트를 세고, MAX에 카운트가 가장 많은 것을 저장한다.
딕셔너리를 돌면서 MAX와 같은 숫자만큼 카운트를 가진 원소를 answer에 저장한다.
from itertools import combinations
def solution(orders, course):
answer = []
for c in course:
dic = dict()
MAX = 0
for order in orders:
if len(order) >= c:
combi = list(combinations(order, c))
for a in combi:
s = ''.join(sorted(list(a)))
if s not in dic: dic[s] = 1
else:
dic[s] += 1
MAX = max(MAX, dic[s])
for key in dic:
if dic[key] == MAX:
answer.append(key)
return sorted(answer)
다른 풀이
여기서는 카운트를 셀 때 Counter 함수를 사용하였다. Counter 함수의 most_common()을 사용하여 가장 많이 사용되었을 때 몇 개인지 구하였다.
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) ]
총평
무난무난
'알고리즘 > 카카오 EASY' 카테고리의 다른 글
실패율 (Level 1) (0) | 2022.06.28 |
---|---|
괄호 변환 (Level 2) (0) | 2022.06.25 |
크레인 인형뽑기 게임 (Level 1) (0) | 2022.06.24 |
키패드 누르기 (Level 1) (0) | 2022.05.26 |
숫자 문자열과 영단어 (Level 1) (0) | 2022.05.25 |