๐ป Coding Problems Solving/DFS | BFS | Backtracking
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ํผ๋ก๋ (์๋ฐ java)
Kim_dev
2023. 3. 1. 22:34
+ ์์ ํ์ ๋ฌธ์
1. ๋ฌธ์ :https://school.programmers.co.kr/learn/courses/30/lessons/87946
2. ํ์ด
๋ฐฐ์ด 123,231 ๋ฑ์ผ๋ก ๋๋ ๋ฒ ์์๋ฌ์ผ๊ฒ ๋ค!
3. ์ฝ๋
class Solution {
static boolean[] visited;
static int count = 0;
public int solution(int k, int[][] dungeons) {
visited = new boolean[dungeons.length];
dfs(0, k, dungeons);
return count;
}
private void dfs(int depth, int fatigue, int[][] dungeons){
for (int i = 0; i < dungeons.length; i++){
if (visited[i] || dungeons[i][0] > fatigue) {
continue;
}
visited[i] = true;
dfs(depth + 1, fatigue - dungeons[i][1], dungeons);
visited[i] = false;
}
count = Math.max(count, depth);
}
}