Problem Solving/Programmers

[Level 2] 피로도

kmkunk 2022. 4. 6. 12:52
class Solution {
    public boolean[] visited;
    public int answer = 0;
    
    public int solution(int k, int[][] dungeons) {
        visited = new boolean[dungeons.length];
        
        recur(0, k, dungeons, 0);
        
        return answer;
    }
    
    public void recur(int index, int k, int[][] dungeons, int count) {
        if(index==dungeons.length) {
            answer = Math.max(answer, count);
            
            return;
        }
        
        for(int i=0; i<dungeons.length; i++) {
            if(!visited[i] || k>=dungeons[i][0]) {
                visited[i] = true;
                recur(index+1, k-dungeons[i][1], dungeons, count+1);
                recur(index+1, k, dungeons, count);
                visited[i] = false;
            }
        }
    }
}