반응형
문제
2021 카카오 블라인드 채용 - 메뉴 리뉴얼
https://school.programmers.co.kr/learn/courses/30/lessons/72411
풀이
설명
새로 추가하게 될 코스요리의 메뉴 구성을 구하는 문제입니다.
메뉴 개수 별로 최소 2번 이상 주문된 단품메뉴 조합 중 가장 많이 주문된 조합을 찾아야 합니다.
그런 조합이 여러 개라면 전부 포함합니다.
예시 코드
function solution(orders, course) {
const combinationCountsByMenuSize = new Map();
const result = [];
for (const c of course) {
combinationCountsByMenuSize.set(c, new Map());
}
for (const order of orders) {
const sortedOrder = order.split("").sort();
for (const c of course) {
const combinations = getCombinations(sortedOrder, c);
for (const comb of combinations) {
const combinationCounts = combinationCountsByMenuSize.get(c);
const key = comb.join("");
const count = (combinationCounts.get(key) || 0) + 1;
combinationCounts.set(key, count);
}
}
}
for (const c of course) {
let maxCount = 0;
const combinationCounts = combinationCountsByMenuSize.get(c);
for (const [key, count] of combinationCounts) {
maxCount = Math.max(maxCount, count);
}
if (maxCount <= 1) continue;
for (const [key, count] of combinationCounts) {
if (count === maxCount) result.push(key);
}
}
return result.sort();
}
'Coding Problem' 카테고리의 다른 글
[프로그래머스] 카카오 - 합승 택시 요금 (0) | 2025.01.19 |
---|---|
[프로그래머스] 카카오 - 후보키 (0) | 2025.01.18 |
[프로그래머스] 카카오 - 보석 쇼핑 (2) | 2025.01.10 |
[프로그래머스] 카카오 - 불량 사용자 (0) | 2025.01.08 |
[프로그래머스] 최고의 집합 (0) | 2025.01.07 |