csct3434
[level 2] 피로도 - 87946 본문
import java.util.stream.IntStream;
class Solution {
private int answer = 0;
public int solution(int k, int[][] dungeons) {
int total = dungeons.length;
boolean[] visited = new boolean[total];
solve(total, 0, 0, visited, dungeons, k);
return answer;
}
private void solve(int total, int count, int repetition, boolean[] visited, int[][] dungeons, int pirodo) {
boolean canGoAnywhere = IntStream.range(0, total)
.anyMatch(i -> !visited[i] && dungeons[i][0] <= pirodo);
if (total == repetition || !canGoAnywhere) {
answer = Math.max(answer, count);
return;
}
for (int i = 0; i < total; i++) {
if (!visited[i] && dungeons[i][0] <= pirodo) {
visited[i] = true;
solve(total, count + 1, repetition + 1, visited, dungeons, pirodo - dungeons[i][1]);
visited[i] = false;
}
}
}
}
'프로그래머스' 카테고리의 다른 글
[level 2] 우박수열 정적분 - 134239 (0) | 2024.02.29 |
---|---|
[level 2] 할인 행사 - 131127 (0) | 2024.02.29 |
[level 2] n^2 배열 자르기 - 87390 (0) | 2024.02.29 |
[level 2] 전력망을 둘로 나누기 - 86971 (1) | 2024.02.29 |
[level 2] 모음 사전 - 84512 (1) | 2024.02.29 |