링크 : https://school.programmers.co.kr/learn/courses/30/lessons/87946
문제 풀이
dfs를 이용해 가능한 모든 경우의 수를 구하면서 가장 큰 depth를 구하도록 함
function dungeons_search(depth, k, dungeons, visited) {
let max_depth = depth;
for (let i = 0; i < dungeons.length; i++) {
if (!visited[i] && dungeons[i][0] <= k) {
visited[i] = true;
const cal_k = k - dungeons[i][1];
const current_depth = dungeons_search(depth + 1, cal_k, dungeons, visited);
max_depth = Math.max(max_depth, current_depth);
visited[i] = false;
}
}
return max_depth;
}
function solution(k, dungeons) {
let visited = new Array(dungeons.length).fill(false);
return dungeons_search(0, k, dungeons, visited);
}
결론
solution내부에 dfs 함수를 정의하여 k, dungeons, visited같은 변수를 전역으로 설정할 경우 굳이 파라미터로 넘길 필요가 없음
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
프로그래머스 모의고사 (0) | 2025.04.24 |
---|---|
프로그래머스 [1차] 캐시 (0) | 2025.04.24 |
프로그래머스 기능개발 (0) | 2025.04.22 |
프로그래머스 의상 (0) | 2025.04.14 |
프로그래머스 폰켓몬 (0) | 2025.04.14 |