알고리즘/프로그래머스
[2021 KAKAO BLIND RECURITMENT] 메뉴 리뉴얼
현진이에오
2022. 5. 7. 02:13
코스개수를 순회하면서 주문할 수 있는 개수에 따른 combination을 만들어주고 가장 많이 나온 아이를 answer에 넣어주는 방법.
from itertools import combinations
from collections import Counter
def solution(orders, course):
answer = []
for c in course:
temp = []
for order in orders:
combi = combinations(sorted(order), c)
temp += combi
counter = Counter(temp)
if len(counter) != 0 and max(counter.values()) != 1:
answer += [''.join(f) for f in counter if counter[f] == max(counter.values())]
return sorted(answer)
JavaScript
function solution (orders, course) {
const ordered = {};
const candidates = {};
const maxNum = Array(10 + 1).fill(0)
const createSet = (arr, start, len, foods) => {
if (len == 0) {
ordered[foods] = (ordered[foods] || 0) + 1;
if (ordered[foods] > 1) candidates[foods] = ordered[foods]
maxNum[foods.length] = Math.max(maxNum[foods.length], ordered[foods])
return
}
for (let i = start; i < arr.length; i++) {
createSet(arr, i+1, len-1, foods + arr[i])
}
}
orders.forEach((order) => {
const sorted = order.split('').sort()
course.forEach((len) => {
createSet(sorted, 0, len, '')
})
})
const launched = Object.keys(candidates).filter((food) => maxNum[food.length] === candidates[food])
return launched.sort()
}