๋ฐ˜์‘ํ˜•

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ ˆ๋ฒจ 2 ํ”ผ๋กœ๋„ ๋ฌธ์ œ๋Š” ๋˜์ „ ๋ชฉ๋ก๊ณผ HP๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋˜์ „์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ ๋˜์ „์€ ์ตœ์†Œ ํ”ผ๋กœ๋„์™€ ์†Œ๋ชจ ํ”ผ๋กœ๋„๋ฅผ ๊ฐ€์ง„๋‹ค. ์ตœ์†Œ ํ”ผ๋กœ๋„๋Š” ํ•ด๋‹น ๋˜์ „์„ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œ HP๋ฅผ ์˜๋ฏธํ•˜๊ณ , ์†Œ๋ชจ ํ”ผ๋กœ๋„๋Š” ๋ง ๊ทธ๋Œ€๋กœ ํ•ด๋‹น ๋˜์ „์„ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ ์†Œ๋ชจ๋˜๋Š” HP๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง€๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

  • k : ์‹œ์ž‘ HP. e.g., 80
  • dungeons : [์ตœ์†Œ ํ”ผ๋กœ๋„, ์†Œ๋ชจ ํ”ผ๋กœ๋„]๋กœ ์ด๋ฃจ์–ด์ง„ ๋˜์ „ ๋ชฉ๋ก. e.g., [[80,20],[50,40],[30,10]]

 

์ตœ์†Œ ํ”ผ๋กœ๋„์™€ ์†Œ๋ชจ ํ”ผ๋กœ๋„๊ฐ€ ๊ฐ๊ฐ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฉ๋ฌธ ์ˆœ์„œ์— ๋”ฐ๋ผ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ๋˜์ „์˜ ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด [[80,20],[50,40],[30,10]] ๋˜์ „ ๋ชฉ๋ก์—์„œ 2~3๋ฒˆ(i1~i2) ๋˜์ „๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•˜๋ฉด ์ตœ๋Œ€ 2๊ฐœ ๋˜์ „๋งŒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค. 1๋ฒˆ ๋˜์ „(i0)์˜ ์ตœ์†Œ ํ”ผ๋กœ๋„๊ฐ€ 80์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋˜์ „์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๋ฉด HP๊ฐ€ 80 ๋ฏธ๋งŒ์ด ๋˜๊ณ  ๊ทธ๋Ÿผ 1๋ฒˆ ๋˜์ „์˜ ์ตœ์†Œ ํ”ผ๋กœ๋„๋ฅผ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

  1. ์ฒซ ๋ฒˆ์งธ ๋˜์ „๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ : 1 → 2 → 3 (์ตœ๋Œ€ 3๊ฐœ ๋˜์ „)
  2. ๋‘ ๋ฒˆ์งธ ๋˜์ „๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ : 2 → 3 (์ตœ๋Œ€ 2๊ฐœ ๋˜์ „)
  3. ์„ธ ๋ฒˆ์งธ ๋˜์ „๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ : 3 → 2 (์ตœ๋Œ€ 2๊ฐœ ๋˜์ „)

 

 

์ˆœ์—ด์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•


์ฃผ์–ด์ง„ ๋˜์ „์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์ˆœ์„œ๋ฅผ ํƒ์ƒ‰ํ•ด๋ด์•ผ ํ•˜๋ฏ€๋กœ ๋˜์ „ ๋ชฉ๋ก์— ๋Œ€ํ•œ ์ˆœ์—ด(Permutation)์„ ์ƒ์„ฑํ•œ ๋’ค, ๊ฐ ์ˆœ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋˜์ „ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐํ–ˆ๋‹ค.

const getPermutation = (arr, perm = [], max = 3) => {
  if (perm.length === max) return [perm];

  const result = [];
  for (let i = 0; i < arr.length; i++) {
    const newPerm = [...perm, arr[i]];
    const rest = arr.slice(0, i).concat(arr.slice(i + 1));
    result.push(...getPermutation(rest, newPerm, max));
  }

  return result;
};

function solution(k, dungeons) {
  const perms = getPermutation(dungeons, [], dungeons.length);
  let maxCount = 0;

  for (const perm of perms) {
    let currentK = k;
    let count = 0;
    for (const [requiredK, consumeK] of perm) {
      if (currentK >= requiredK) {
        currentK -= consumeK;
        count++;
      } else {
        break;
      }
    }
    maxCount = Math.max(count, maxCount);
  }

  return maxCount;
}

 

๋ชจ๋“  ์ˆœ์—ด์˜ ๊ฐœ์ˆ˜๋Š” $n!$ ์ด๋ฏ€๋กœ, getPermutation ํ•จ์ˆ˜์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” $O(n!)$ ์ด ๋œ๋‹ค. solution ํ•จ์ˆ˜์—์„  ์ƒ์„ฑํ•œ ์ˆœ์—ด์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ˆœํšŒํ•˜๋ฏ€๋กœ ์ „์ฒด ์ฝ”๋“œ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” $O(n! \cdot n)$์œผ๋กœ ๋น„ํšจ์œจ์ ์ด๋‹ค. ๋งŒ์•ฝ n์ด 10์ด๋ผ๋ฉด ์•ฝ 3600๋งŒ ๋ฒˆ์˜ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•˜๋‹ค.

 

 

๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•


๐Ÿ’ก ๋ฐฑํŠธ๋ž˜ํ‚น, ์ˆœ์—ด์€ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋‹ต์„ ์ฐพ๋Š” ์™„์ „ ํƒ์ƒ‰(Exhaustive Search)์— ์†ํ•œ๋‹ค.

 

๋ฐฑํŠธ๋ž˜ํ‚น์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์‹œ๋„ํ•˜๋ฉด์„œ, ์กฐ๊ฑด์— ๋งž์ง€ ์•Š์œผ๋ฉด ํƒ์ƒ‰์„ ์ค‘๋‹จํ•˜๊ณ  ์ด์ „ ๋‹จ๊ณ„๋กœ ๋Œ์•„๊ฐ€์„œ ๋‹ค๋ฅธ ๊ฐ€๋Šฅ์„ฑ์„ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋ถˆํ•„์š”ํ•œ ๊ฒฝ๋กœ๋ฅผ ๋ฐฐ์ œํ•˜๋Š” ๊ฐ€์ง€์น˜๊ธฐ(pruning) ๊ณผ์ •์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ๋” ํšจ์œจ์ ์œผ๋กœ ํ•ด๊ฒฐํ•œ๋‹ค.

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ์ข‹์•„์š”๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ›์€ ์ฝ”๋“œ๋Š” ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค. ์ด ๋ฌธ์ œ๋Š” ์ˆœ์—ด์„ ๊ณ ๋ คํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ $O(n!)$์ด์ง€๋งŒ ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊น”๋”ํ•ด์ง„๋‹ค.

function solution(k, dungeons) {
  const N = dungeons.length; // ๋˜์ „ ๊ฐœ์ˆ˜
  const visited = new Array(N).fill(false); // ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ถ”์ ํ•˜๋Š” ๋ฐฐ์—ด
  let maxCount = 0;

  function dfs(hp, count) {
    maxCount = Math.max(count, maxCount);

    for (let i = 0; i < N; i++) {
      // ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋˜์ „์ด๊ณ , ๋˜์ „์˜ ์ตœ์†Œ ํ”ผ๋กœ๋„๋ฅผ ๋งŒ์กฑํ•˜๋ฉด
      if (hp >= dungeons[i][0] && !visited[i]) {
        visited[i] = true; // ๋ฐฉ๋ฌธ ์ƒํƒœ๋กœ ๋ณ€๊ฒฝ
        dfs(hp - dungeons[i][1], count + 1);
        visited[i] = false; // ๋ฏธ๋ฐฉ๋ฌธ ์ƒํƒœ๋กœ ๋ณ€๊ฒฝ (๋ฐฑํŠธ๋ž˜ํ‚น)
      }
    }
  }

  dfs(k, 0);
  return maxCount;
}

 

์œ„ ์ฝ”๋“œ์˜ ์ž‘๋™ ๊ณผ์ •์„ 4๋‹จ๊ณ„๋กœ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  1. ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋˜์ „์ด๊ณ , ๋˜์ „์˜ ์ตœ์†Œ ํ”ผ๋กœ๋„๋ฅผ ๋งŒ์กฑํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.
  2. ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด count, hp ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ  ๋˜์ „์„ ๋ฐฉ๋ฌธํ•œ๋‹ค.
  3. ์ตœ๋Œ€ ๋ฐฉ๋ฌธ ์นด์šดํŠธ(maxCount)๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
  4. ๋ฐฉ๋ฌธ ์ƒํƒœ๋ฅผ ๋˜๋Œ๋ฆฌ๊ณ (๋ฐฑํŠธ๋ž˜ํ‚น), ๋‹ค์Œ ๋˜์ „์„ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

k=80, dungeons=[[80,20],[50,40],[30,10]] ์ผ ๋•Œ ๋™์ž‘ ๊ณผ์ • ์‹œ๊ฐํ™”. ๋ชจ๋“  ๋ฐฉ๋ฌธ๋งˆ๋‹ค N ๋งŒํผ ์ˆœํšŒํ•œ๋‹ค

๋”๋ณด๊ธฐ
k = 80, dungeons = [[80, 20], [50, 40], [30, 10]]
N = 3 | visited = [f, f, f] | maxCount = 0
===================================================================

โ‘ด dfs(80, 0)
maxCount = max(0, 0) = 0
i0 -> โ‘ต dfs(80-20, 0+1) | visited = [t, f, f] -> visited = [f, f, f]
i1 -> โ‘น dfs(80-40, 0+1) | visited = [f, t, f] -> visited = [f, f, f]
i2 -> โ‘ป dfs(80-10, 0+1) | visited = [f, f, t] -> visited = [f, f, f]

================================ i0 ================================
โ‘ต dfs(60, 1)
maxCount = max(1, 0) = 1
i0 -> pass(์ด๋ฏธ ๋ฐฉ๋ฌธ)
i1 -> โ‘ถ dfs(60-40, 1+1) | visited = [t, t, f] -> visited = [t, f, f]
i2 -> โ‘ท dfs(60-10, 1+1) | visited = [t, f, t] -> visited = [t, f, f]

โ‘ถ dfs(20, 2)
maxCount = max(2, 1) = 2
i0~i2 pass

โ‘ท dfs(50, 2)
maxCount = max(2, 2) = 2
i0 -> pass(์ด๋ฏธ ๋ฐฉ๋ฌธ)
i1 -> โ‘ธ dfs(50-40, 2+1) | visited = [t, t, t] -> visited = [t, f, t]
i2 -> pass(์ด๋ฏธ ๋ฐฉ๋ฌธ)

โ‘ธ dfs(10, 3)
maxCount = max(3, 2) = 3
i0~i2 pass

================================ i1 ================================
โ‘น dfs(40, 1)
maxCount = max(1, 3) = 3
i0 -> pass(ํ”ผ๋กœ๋„ ๋งŒ์กฑ ๋ชปํ•จ)
i1 -> pass(์ด๋ฏธ ๋ฐฉ๋ฌธ)
i2 -> โ‘บ dfs(40-10, 1+1) | visited = [f, t, t] -> visited = [f, t, f]

โ‘บ dfs(30, 2)
maxCount = max(2, 3) = 3
i0~i2 pass

================================ i2 ================================
โ‘ป dfs(70, 1)
maxCount = max(1, 3) = 3
i0 -> pass(ํ”ผ๋กœ๋„ ๋งŒ์กฑ ๋ชปํ•จ)
i1 -> โ‘ผ dfs(70-40, 1+1) | visited = [f, t, t] -> visited = [f, f, t]
i2 -> pass(์ด๋ฏธ ๋ฐฉ๋ฌธ)

โ‘ผ dfs(30, 2)
maxCount = max(1, 3) = 3
i0~i2 pass

 

(๋ฒˆ์™ธ) ๋ถ€๋ถ„์ง‘ํ•ฉ ์ฐพ๊ธฐ


๋ถ€๋ถ„์ง‘ํ•ฉ์€ ์–ด๋–ค ์ง‘ํ•ฉ์˜ ์›์†Œ๋“ค ์ค‘ ์ผ๋ถ€ ๋˜๋Š” ์ „์ฒด๋ฅผ ํฌํ•จํ•˜๋Š” ์ง‘ํ•ฉ์„ ์˜๋ฏธํ•œ๋‹ค. ์ง‘ํ•ฉ A์˜ ๋ชจ๋“  ์›์†Œ๊ฐ€ ์ง‘ํ•ฉ B์— ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด, ์ง‘ํ•ฉ A๋Š” ์ง‘ํ•ฉ B์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ(Subset)์ด ๋˜๋ฉฐ, ๊ธฐํ˜ธ๋Š” $A \subset B$๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

๋ถ€๋ถ„์ง‘ํ•ฉ์€ ์›์†Œ ๊ฐœ์ˆ˜ 0๊ฐœ๋ถ€ํ„ฐ ์ตœ๋Œ€ ์›์†Œ ๊ฐœ์ˆ˜๊นŒ์ง€ ํฌํ•จํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ณต์ง‘ํ•ฉ๊ณผ ์ž๊ธฐ ์ž์‹ ๋„ ๋ถ€๋ถ„์ง‘ํ•ฉ์— ํฌํ•จ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ง‘ํ•ฉ {1, 2, 3, 4}์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

  1. ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 0๊ฐœ์ธ ๋ถ€๋ถ„์ง‘ํ•ฉ: ๊ณต์ง‘ํ•ฉ $\emptyset$
  2. ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 1๊ฐœ์ธ ๋ถ€๋ถ„์ง‘ํ•ฉ(1์ž๋ฆฌ ์กฐํ•ฉ): {1}, {2}, {3}, {4}
  3. ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ์ธ ๋ถ€๋ถ„์ง‘ํ•ฉ(2์ž๋ฆฌ ์กฐํ•ฉ): {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}
  4. ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ์ธ ๋ถ€๋ถ„์ง‘ํ•ฉ(3์ž๋ฆฌ ์กฐํ•ฉ): {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}
  5. ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 4๊ฐœ์ธ ๋ถ€๋ถ„์ง‘ํ•ฉ(์ž๊ธฐ ์ž์‹ , 4์ž๋ฆฌ ์กฐํ•ฉ): {1, 2, 3, 4}

 

๊ฐ ์›์†Œ๋Š” ๋ถ€๋ถ„์ง‘ํ•ฉ์— ํฌํ•จ๋˜๊ฑฐ๋‚˜ ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๋‘ ๊ฐ€์ง€ ์„ ํƒ์„ ๊ฐ€์ง€๋ฏ€๋กœ, n๊ฐœ ์›์†Œ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ์ด๊ฐœ์ˆ˜๋Š” $2 \times 2 \times \dots \times 2 = 2^n$๊ฐœ๊ฐ€ ๋œ๋‹ค.

 

์ด์ฒ˜๋Ÿผ ๋ถ€๋ถ„์ง‘ํ•ฉ์€ ์ฃผ์–ด์ง„ ์ง‘ํ•ฉ์˜ ์›์†Œ๋“ค๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ(Combination)์„ ์˜๋ฏธํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ํ•œํŽธ, ํŠน์ • ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ์ฐพ์„ ๋•Œ๋„ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ์ด๊ฐœ์ˆ˜์™€ ๋™์ผํ•œ $O(2^n)$์ด ๋œ๋‹ค.

function findSubsets(nums) {
  const result = [];

  function generateSubsets(start, path) {
    result.push([...path]); // ํ˜„์žฌ ๊ฒฝ๋กœ๋ฅผ ๊ฒฐ๊ณผ์— ์ถ”๊ฐ€

    for (let i = start; i < nums.length; i++) {
      path.push(nums[i]); // ํ˜„์žฌ ์›์†Œ๋ฅผ ๊ฒฝ๋กœ์— ์ถ”๊ฐ€
      generateSubsets(i + 1, path); // ๋‹ค์Œ ์›์†Œ ํƒ์ƒ‰
      path.pop(); // ๊ฒฝ๋กœ์—์„œ ๋งˆ์ง€๋ง‰ ์›์†Œ ์ œ๊ฑฐ(๋ฐฑํŠธ๋ž˜ํ‚น)
    }
  }

  generateSubsets(0, []);
  return result;
}

 

  1. ํ˜„์žฌ ๊ฒฝ๋กœ์˜ ๋ณต์‚ฌ๋ณธ์„ result์— ์ถ”๊ฐ€ํ•œ๋‹ค
  2. start ์ธ๋ฑ์Šค์— ์žˆ๋Š” ์›์†Œ๋ถ€ํ„ฐ path์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋‹ค์Œ ์›์†Œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค
  3. ํƒ์ƒ‰์„ ๋งˆ์น˜๋ฉด ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ path์—์„œ ์ œ๊ฑฐํ•œ๋‹ค(๋ฐฑํŠธ๋ž˜ํ‚น)
  4. nums ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๊นŒ์ง€ ์œ„ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

nums=[1, 2, 3, 4] ์ผ ๋•Œ ๋™์ž‘ ๊ณผ์ • ์‹œ๊ฐํ™”

๋”๋ณด๊ธฐ
nums = [1, 2, 3]
==============================================================

f(0, [])
result = [[]]
i0 -> path = [1] -> โ‘ด f(0+1, [1]) -> path = []
i1 -> path = [2] -> โ‘ธ f(1+1, [2]) -> path = []
i2 -> path = [3] -> โ‘บ f(2+1, [3]) -> path = []

โ‘ด f(1, [1])
result = [[], [1]]
i1 -> path = [1, 2] -> โ‘ต f(1+1, [1, 2]) -> path = [1]
i2 -> path = [1, 3] -> โ‘ท f(2+1, [1, 3]) -> path = [1]

โ‘ต f(2, [1, 2])
result = [[], [1], [1, 2]]
i2 -> path = [1, 2, 3] -> โ‘ถ f(2+1, [1, 2, 3]) -> path = [1, 2]

โ‘ถ f(3, [1, 2, 3])
result = [[], [1], [1, 2], [1, 2, 3]]
skip loop

โ‘ท f(3, [1, 3])
result = [[], [1], [1, 2], [1, 2, 3], [1, 3]]
skip loop

โ‘ธ f(2, [2])
result = [[], [1], [1, 2], [1, 2, 3], [1, 3], [2]]
i2 -> path = [2, 3] -> โ‘น f(2+1, [2, 3]) -> path = [2]

โ‘น f(3, [2, 3])
result = [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3]]
skip loop

โ‘บ f(3, [3])
result = [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
skip loop

๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•