카테고리 없음

[다시풀기/javascript/프로그래머스] 메뉴리뉴얼

_서리__ 2023. 3. 21. 14:35
function solution(orders, course) {
    var answer = [];
    for(let i=0;i<course.length;i++){
        let obj = {}
        let max = 0;
        for(let j=0;j<orders.length;j++)
            combi(course[i],orders[j]).map((el)=>{
                let newWord =  el.sort().join('')
                if(obj[newWord]){
                    obj[newWord]++;
                }
                else obj[newWord] = 1;
                max = Math.max(max,obj[newWord])
            });
        let addAnswer = [];
        if(max>=2){
            for(let key in obj){
                if(obj[key]===max){
                    addAnswer.push(key)
                }
            }
        }
        answer.push(...addAnswer)
    }
    return answer.sort()
}

function combi(len,arr){
    const answer = [];
    const temp = Array(len);
    function DFS(L,index){
        if(L===len) answer.push(temp.slice())
        else{
        for(let i=index;i<arr.length;i++){
            temp[L] = arr[i];
            DFS(L+1,i+1);
        }
        }
    }
    DFS(0,0)
    return answer;
}

조합을 확인하는 방법을 알아야 풀 수 있는 문제였다.