๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/87946
์นดํ ๊ณ ๋ฆฌ: ์์ ํ์
์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉ ํ ์คํธ ๊ณ ๋์ Kit, https://school.programmers.co.kr/learn/challenges?tab=algorithm_practice_kit
DFS๋ฅผ ์ด์ฉํด ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ค ์ง์ ๊ณ์ฐํ ๊ฒฐ๊ณผ๋ฅผ answer ๋ฐฐ์ด์ ์ ์ฅํด๋๊ณ , answer ๋ฐฐ์ด์ ์ต๋๊ฐ์ ๋ฆฌํดํ๋ค.
function solution(k, dungeons) {
let answer = [];
let visited = Array(dungeons.length).fill(0);
function dfs(cnt, k) {
answer.push(cnt);
for (let i = 0; i < dungeons.length; i++) {
let current = dungeons[i];
if (k >= current[0] && !visited[i]) {
visited[i] = 1;
dfs(cnt + 1, k - current[1]);
visited[i] = 0;
}
}
}
dfs(0, k);
return Math.max(...answer);
}