[프로그래머스] Lv2. 메뉴 리뉴얼- JavaScript

이상돈·2023년 5월 2일
0
post-thumbnail

문제분류 : 코팅테스트 연습

난이도 : Level 2

출처 : 프로그래머스 - 메뉴 리뉴얼

문제

제한사항

📌 내가 생각한 풀이

조합을 이용하여, 각 course마다의 조합의 경우의 수를 모두 구하고, 객체를 이용하여, 겹치는 couse가 몇개인지 센다(이때, sort로 정렬을 해주어야 "XY", "YX"가 같은것으로 취급된다.) 가장 많은 개수를 세주고, answer에 push한 다음 sort하여 리턴한다.
//조합을 이용해서, 코스요리 개수개만큼 각각 찾자

const combination = (arr, selectNum) =>{
    let result = [];
    if(selectNum === 1) return arr.map((d)=>[d]);
    arr.forEach((data,idx,origin)=>{
        let rest = origin.slice(idx+1);
        let combi = combination(rest,selectNum-1);
        let attached = combi.map((d)=>[data, ...d]);
        result.push(...attached);
    })
    return result;
    
}


function solution(orders, course) {
    var answer = [];
    for(var i =0; i<course.length; i++){
        let chooseNum = course[i];
        let obj = {}
        orders.map((data)=>{
            let splited = data.split('').sort();
            let wholeCase = combination(splited, chooseNum);
            wholeCase.map((d)=>{
                let str = d.join('');
                obj[str] === undefined ? obj[str]=1 : obj[str]+=1;
            })
        })
        let entries = Object.entries(obj);
        entries.sort((a,b)=>b[1] - a[1])
        if(entries.length >= 2){
            let maxNum = entries[0][1]
            let newArr = entries.filter((data)=> data[1] != 1).filter((d)=>d[1] == maxNum).map((da)=>answer.push(da[0]))}
    }
    return answer.sort()
}

📌 느낀점

카카오의 문제는, 보기엔 매우 어려워 보인다. 하지만 문제에 제한사항, 조건들이 매우 상세히 쓰여져있으므로, 구현문제 같은경우에는,꼼꼼히 문제를 읽고 해결하기만 하면 된다!

profile
사람들의 더 나은 삶을 위한 개발자

0개의 댓글