๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐Ÿ–ฅ dev-ruby

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์–‘๊ณผ ๋Š‘๋Œ€ | ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ | Lv.3 | 2022 KAKAO BLIND RECRUITMENT | DFS ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์–‘๊ณผ ๋Š‘๋Œ€ | ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ | Lv.3 | 2022 KAKAO BLIND RECRUITMENT | DFS

ruby_s 2022. 5. 6. 12:58
728x90
๋ฐ˜์‘ํ˜•
SMALL

๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/92343

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์–‘๊ณผ ๋Š‘๋Œ€

[0,0,1,1,1,0,1,0,1,0,1,1] [[0,1],[1,2],[1,4],[0,8],[8,7],[9,10],[9,11],[4,3],[6,5],[4,6],[8,9]] 5 [0,1,0,1,1,0,1,0,0,1,0] [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6],[3,7],[4,8],[6,9],[9,10]] 5

programmers.co.kr

 

ํ’€์ด

function solution(info, edges) {
  const tree = Array.from({ length: info.length }, () => []);
  for (const [parent, child] of edges) tree[parent].push(child);

  let answer = 0;
  function dfs(node, sheep, wolf, possible) {
    answer = Math.max(answer, sheep);
    if (sheep <= wolf) return;

    const possibleNode = [...possible, ...tree[node]];
    possibleNode.splice(possibleNode.indexOf(node), 1);

    for (const next of possibleNode) {
      if (info[next]) dfs(next, sheep, wolf + 1, possibleNode);	// ๋Š‘๋Œ€์ผ ๊ฒฝ์šฐ
      else dfs(next, sheep + 1, wolf, possibleNode);	// ์–‘์ผ ๊ฒฝ์šฐ
    }
  }
  dfs(0, 1, 0, [0]);

  return answer;
}

์ด ๋ฌธ์ œ ๋„ˆ๋ฌด ์–ด๋ ค์› ๋‹ค.. ๊ทธ๋ƒฅ dfs๋กœ ํ•˜๋ฉด ์ด์ „์— ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ณณ์„ ์–ด๋–ป๊ฒŒ ๋˜ ๋ฐฉ๋ฌธํ•˜์ง€ ..? ์‹ถ์—ˆ๋‹ค..

ํ•˜๋‚˜์”ฉ ํ’€์ดํ•ด๋ณด๋ฉด, ๋จผ์ € parent์˜ ์ธ๋ฑ์Šค์— ๊ฐ๊ฐ [์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ] ์ž์‹์„ ๋„ฃ์–ด์ค€๋‹ค. -> tree
์•„๋ž˜ ์˜ˆ์‹œ ์‚ฌ์ง„์œผ๋กœ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

์œ„์—์„œ ๋ณด๋ฉด  0 -> 1 -> 8 -> 7 -> 9 -> 4 -> 6 -> 5 ์ด ์ˆœ์„œ๋Œ€๋กœ ๊ฐ€์•ผ ์–‘์˜ ์ตœ๋Œ€๊ฐ’์ด ๋‚˜์˜จ๋‹ค. 

function dfs(node, sheep, wolf, possible) {
    answer = Math.max(answer, sheep);
    if (sheep <= wolf) return;

    const possibleNode = [...possible, ...tree[node]];
    possibleNode.splice(possibleNode.indexOf(node), 1);

    for (const next of possibleNode) {
      if (info[next]) dfs(next, sheep, wolf + 1, possibleNode);
      else dfs(next, sheep + 1, wolf, possibleNode);
    }
}

dfs๋ฅผ ๋“ค์–ด์˜ค๋ฉด sheep๊ฐœ์ˆ˜๋ฅผ ๊ณ„์†ํ•ด์„œ ๋ฐ˜์˜ํ•ด์ค€๋‹ค. ๋งŒ์•ฝ sheep๋ณด๋‹ค wolf์˜ ๊ฐœ์ˆ˜๊ฐ€ ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋ฆฌํ„ดํ•œ๋‹ค.

์ด์ œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด์˜จ possible๊ณผ ํ˜„์žฌ node์˜ ์ž์‹๋…ธ๋“œ๋“ค์„ ํ•ฉ์ณ์ค„ ๊ฒƒ์ด๋‹ค. possible์€ ๋ง ๊ทธ๋Œ€๋กœ ์•„์ง๊นŒ์ง„ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ๋…ธ๋“œ๋“ค์„ ๋œปํ•œ๋‹ค. ๋งŒ์•ฝ 2๋…ธ๋“œ๋ฅผ ๋“ค์–ด๊ฐ”๋‹ค ์™”์–ด๋„ wolf์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์„œ ์ข…๋ฃŒ๋๋‹ค๋ฉด 2๋Š” ์•„์ง ๋ฐฉ๋ฌธ์˜ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๋…ธ๋“œ๊ฐ€ ๋œ๋‹ค. -> possibleNode๋ฅผ ๋ฐ˜๋ณต๋ฌธ ๋ˆ๋‹ค๊ณ  ํ•ด์„œ ๊ผญ ๋…ธ๋“œ๊ฐ€ ์‚ฌ๋ผ์ง€๋Š”๊ฒŒ ์•„๋‹˜. (๋‚˜์˜ ํฐ ์ฐฉ๊ฐ์ด์˜€์Œ. dfs๋ฅผ ๋“ค์–ด๊ฐ”๋‹ค์˜ค๋ฉด ๊ฐ ๋…ธ๋“œ๊ฐ€ ๊ผญ ์‚ฌ๋ผ์ง„๋‹ค๊ณ  ์ธ์ง€ํ•ด๋ฒ„๋ ธ๋‹ค. if (sheep <= wolf) return; ์ด ์กฐ๊ฑด์— ๊ฑธ๋ฆฌ๋ฉด ์•ˆ์‚ฌ๋ผ์ง.)

์•ž์„œ ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ๋…ธ๋“œ๋“ค๊ณผ ํ˜„์žฌ node์˜ ์ž์‹๋…ธ๋“œ๋ฅผ ํ•ฉ์นœ ๋‹ค์Œ, ํ˜„์žฌ node๋Š” ๋ฐฉ๋ฌธ์„ ํ–ˆ์œผ๋ฏ€๋กœ possibleNode์—์„œ ์ œ๊ฑฐํ•œ๋‹ค.(splice) ๊ทธ๋ฆฌ๊ณค ์–‘์ผ ๊ฒฝ์šฐ์™€ ๋Š‘๋Œ€์ผ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ ์„œ ์นด์šดํŠธ๋ฅผ ์กฐ์ ˆํ•ด์ฃผ๋ฉด ๋œ๋‹ค.


๋ฐฉ๋ฌธํ•œ ํ˜„์žฌ node์™€ possibleNode๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ณด๋Š”๊ฒŒ ์‚ฌ์‹ค ์ดํ•ด๊ฐ€ ๋น ๋ฅด๋‹ค.

// console.log(node, possibleNode)
0, [ 1, 8 ]
1, [ 8, 2, 4 ]
8, [ 2, 4, 7, 9 ]
2
4
7, [ 2, 4, 9 ]
2, [ 4, 9 ]
4
9, [ 4, 10, 11 ]
4, [ 10, 11, 3, 6 ]
10
11
3
6
0: 1, 8์˜ ์ž์‹๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•œ ๋…ธ๋“œ์ด๋‹ค.
1: ๋ณธ์ธ 1์€ ์ œ๊ฑฐํ•˜๊ณ  ์ „๋‹ฌ๋ฐ›์€ 8๊ณผ ๋ณธ์ธ์˜ ์ž์‹๋…ธ๋“œ 2, 4๊ฐ€ ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ๋…ธ๋“œ์ด๋‹ค.
8: ๋ณธ์ธ 8์€ ์ œ๊ฑฐํ•˜๊ณ  ์ „๋‹ฌ๋ฐ›์€ 2, 4์™€ ๋ณธ์ธ์˜ ์ž์‹๋…ธ๋“œ 7, 9๊ฐ€ ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ๋…ธ๋“œ์ด๋‹ค.
2: wolf๊ฐ€ ๋” ์ปค์ง€๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฏ€๋กœ ๋ฐ”๋กœ ๋ฆฌํ„ด๋œ๋‹ค.
4: ๋งˆ์ฐฌ๊ฐ€์ง€.
7: ๋ณธ์ธ 7์€ ์ œ๊ฑฐํ•˜๊ณ , 8๋ฒˆ์งธ ๋…ธ๋“œ์—์„œ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ๋ฐ›์€ 2, 4, 9๊ฐ€ ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ๋…ธ๋“œ์ด๋‹ค.
2: ์œ„์˜ 2์™€ ๋‹ค๋ฅด๊ฒŒ sheep์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด์„œ ์™”์œผ๋ฏ€๋กœ 2๋„ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณธ์ธ2๋Š” ์ œ๊ฑฐํ•˜๊ณ , 7์—์„œ ์ „๋‹ฌ๋ฐ›์€ 4, 9๊ฐ€ ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ๋…ธ๋“œ์ด๋‹ค.
4: return
9: ๋ณธ์ธ 9๋Š” ์ œ๊ฑฐํ•˜๊ณ , 2์—์„œ ์ „๋‹ฌ๋ฐ›์€ 4์™€ ๋ณธ์ธ์˜ ์ž์‹๋…ธ๋“œ 10, 11์ด ๊ฐ€๋Šฅ์„ฑ ์žˆ๋Š” ๋…ธ๋“œ์ด๋‹ค.
4: ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ ์ƒ๋žต ...

 

๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ์‰ฝ๊ฒŒ ํ’€์—ˆ์œผ๋ฉด ํ•œ๋‹ค.. ๋‚œ ์‚ฌ์‹ค ์•„์ง๋„ ์žฌ๊ท€๊ฐ€ ์ต์ˆ™์น˜ ์•Š๋‹ค ..ใ…Ž; ํ‰์ƒ ์นœํ•ด์ง€๊ณ  ์‹ถ์ง€ ์•Š์€ ์นœ๊ตฌ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•
LIST