Coding Problem

[프로그래머스] 카카오 - 메뉴 리뉴얼

Yepchani 2025. 1. 16. 16:00
반응형

문제

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();
}