Coding Test/프로그래머스

[프로그래머스] 피로도 - 자바

lsh2613 2023. 8. 10. 22:24

https://school.programmers.co.kr/learn/courses/30/lessons/87946

 

프로그래머스

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

programmers.co.kr

문제 풀이

dfs()를 통해 k가 모든 던전의 최소 필요 피로도 작아질 때까지 모든 경우를 탐색한다.

 

코드

class Solution {
    static boolean visited[];
    static int answer = 0;
    
    public int solution(int k, int[][] dungeons) {
        visited=new boolean[dungeons.length];
        dfs(k, dungeons, 0);
        
        return answer;
    }
    
    static void dfs(int k, int[][] dungeons, int depth){
        for(int i=0; i<dungeons.length; i++){
            if(visited[i]||dungeons[i][0]>k)
                continue;
            
            visited[i]=true;
            k-=dungeons[i][1];
            dfs(k, dungeons, depth+1);
            visited[i]=false;
            k+=dungeons[i][1];
        }
        answer = java.lang.Math.max(answer, depth);
    }
}

 

느낀점