[JS] 프로그래머스 Lv.2 : 피로도 - DFS

ahyes·2023년 4월 25일
0

던전 array 순서대로 처음부터 탐색 -> 점점 깊게 들어가는 방식

function solution(k, dungeons) {
    let max = 0;
    let visit = new Array(dungeons.length).fill(false);
    
    function dfs(cur,tired,depth){
        visit[cur] = true;
        tired-=dungeons[cur][1];
        for(let i = 0; i < dungeons.length; i++){
            if(!visit[i]&&tired>=dungeons[i][0])dfs(i,tired,depth+1);
        }
        max = Math.max(max,depth);
        visit[cur] = false;
    }
    for(let i = 0 ; i < dungeons.length; i++){
        if(dungeons[i][0]<=k)dfs(i,k,1);
    }
    return max;
}


profile
프론트엔드 공부를 해봅시다

0개의 댓글