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