๐Ÿ”Žํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํ”ผ๋กœ๋„

๋ฐ•๋ฏผ์šฐยท2023๋…„ 7์›” 16์ผ
0

๋ฌธ์ œ: 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);
}
profile
๊พธ์ค€ํžˆ, ๊นŠ๊ฒŒ

0๊ฐœ์˜ ๋Œ“๊ธ€