KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • codeup
  • 백준
  • programmers
  • Level 2
  • 수학
  • 다이나믹 프로그래밍
  • Level 1
  • 제코베 JS 100제
  • 정렬
  • 그래프
  • Level 0
  • Level1
  • js
  • recursion
  • C++
  • 문자열
  • tree
  • LeetCode
  • C
  • string

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun
PS/Programmers

Programmers / Level 2 / 메뉴 리뉴얼 / JS

PS/Programmers

Programmers / Level 2 / 메뉴 리뉴얼 / JS

2023. 1. 17. 23:19

< 문제 바로가기 >

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

/**
 * num개로 이루어진 가능한 모든 조합을 반환하는 함수
 *
 * @param {string[]} order
 * @param {number[]} num 조합을 이룰 order의 개수
 * @returns
 */
function getCombination(order, num) {
  let result = [];

  if (num === 1) return order.map((el) => [el]);

  order.forEach((fixed, idx) => {
    let rest = order.slice(idx + 1);
    let combinationList = getCombination(rest, num - 1);
    let attached = combinationList.map((el) => [fixed, ...el]);

    result.push(...attached);
  });

  return result.map((el) => el.sort().join(''));
}

function solution(orders, course) {
  let result = [];
  let combinationCount = {};

  orders.forEach((order) => {
    course.forEach((num) => {
      getCombination(order.split(''), num).forEach((el) => {
        combinationCount[el] = combinationCount[el] + 1 || 1;
      });
    });
  });

  course.forEach((num) => {
    let maxOrderCount = 0;

    for (const KEY in combinationCount) {
      if (num !== KEY.length) continue;
      if (combinationCount[KEY] > maxOrderCount) {
        maxOrderCount = combinationCount[KEY];
      }
    }

    for (const KEY in combinationCount) {
      if (KEY.length !== num) continue;
      if (combinationCount[KEY] < 2) continue;
      if (combinationCount[KEY] === maxOrderCount) {
        result.push(KEY);
      }
    }
  });

  console.log(combinationCount);
  result = result.sort();
  return result;
}

const orders = ['XYZ', 'XWY', 'WXA'];
const course = [2, 3, 4];

const answer = solution(orders, course);
console.log(answer);

조합을 구현하는것에 익숙하지 않다면 Level 2에서 어려웠던 문제였던 것 같다. (내가 그랬다...)

조합만 구현할 줄 안다면 나머진 구현파트라 크게 어렵진 않았다.

 

course에 담긴 숫자들이 의미하는것은 조합에 들어간 메뉴의 개수인데, 가능한 모든 조합을 객체에 저장한다.

그리고 그 조합들이 몇번 나왔는지 count를 value값으로 가진다.

그 후 문제에서 원하는 개수와 같은 value값을 가진 key값(조합)을 찾아내서 그 중 최댓값을 result 배열에 넣어주면 된다.

저작자표시

'PS > Programmers' 카테고리의 다른 글

Programmers / Level 3 / 줄 서는 방법 / JS  (1) 2023.01.21
Programmers / Level 2 / 2 x n 타일링 / JS  (0) 2023.01.19
Programmers / Level 2 / 피로도 / JS  (0) 2023.01.14
Programmers / Level 0 / 다음에 올 숫자 / JS  (0) 2023.01.11
    'PS/Programmers' 카테고리의 다른 글
    • Programmers / Level 3 / 줄 서는 방법 / JS
    • Programmers / Level 2 / 2 x n 타일링 / JS
    • Programmers / Level 2 / 피로도 / JS
    • Programmers / Level 0 / 다음에 올 숫자 / JS
    KimMinJun
    KimMinJun

    티스토리툴바

    단축키

    내 블로그

    내 블로그 - 관리자 홈 전환
    Q
    Q
    새 글 쓰기
    W
    W

    블로그 게시글

    글 수정 (권한 있는 경우)
    E
    E
    댓글 영역으로 이동
    C
    C

    모든 영역

    이 페이지의 URL 복사
    S
    S
    맨 위로 이동
    T
    T
    티스토리 홈 이동
    H
    H
    단축키 안내
    Shift + /
    ⇧ + /

    * 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.