Logo
Hyunsu Blog
algorithm-tree

๐Ÿ“†Published :Dec 22, 2023 โ€ข

๐Ÿ“†Updated :Dec 22, 2023 โ€ข

โ˜•๏ธ2min

Tree

๊ณจ๋“ ๋ž˜๋น— ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ํ•ฉ๊ฒฉ์ž ๋˜๊ธฐ ํŒŒ์ด์ฌ ํŽธ์˜ 9์žฅ ์จ๋จธ๋ฆฌ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐœ๋… ์ •๋ฆฌ

์ด์ง„ํŠธ๋ฆฌ ํ‘œํ˜„ํ•˜๊ธฐ

  • ์ด์ง„ ํŠธ๋ฆฌ๋Š” ํŠธ๋ฆฌ์˜ ํ•œ ์ข…๋ฅ˜๋กœ ๋ชจ๋“  ๋…ธ๋“œ์˜ ์ตœ๋Œ€ ์ฐจ์ˆ˜๊ฐ€ 2๋ฅผ ๋„˜์ง€ ์•Š๋Š” ํŠธ๋ฆฌ. ์ฆ‰, ์ตœ๋Œ€ 2๊ฐœ์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. (์ž์‹ ๋…ธ๋“œ 0,1,2 ๊ฐ€๋Šฅ)
  • ์ด์ง„ ํŠธ๋ฆฌ๋Š” ๋ฐฐ์—ด์ด๋‚˜ ํฌ์ธํ„ฐ๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅ

์ด์ง„ํŠธ๋ฆฌ ๋ฐฐ์—ด๋กœ ํ‘œํ˜„

  • ๋ฃจํŠธ ๋…ธ๋“œ๋Š” ๋ฐฐ์—ด ์ธ๋ฑ์Šค 1 ๋ฒˆ์— ์ €์žฅ
  • ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ : ๋ถ€๋ชจ ๋…ธ๋“œ ๋ฐฐ์—ด ์ธ๋ฑ์Šค *2
  • ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ : ๋ถ€๋ชจ ๋…ธ๋“œ ๋ฐฐ์—ด ์ธ๋ฑ์Šค *2+1

TIP . ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ ์ธ๋ฑ์Šค๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ ์™ผ์ชฝ์ž์‹ ๋…ธ๋“œ๋Š” ๋ถ€๋ชจ ๋…ธ๋“œ ๋ฐฐ์—ด ์ธ๋ฑ์Šค*2+1, ์˜ค๋ฅธ์ชฝ ์ž์‹๋…ธ๋“œ๋Š” ๋ถ€๋ชจ ๋…ธ๋“œ ๋ฐฐ์—ด ์ธ๋ฑ์Šค*2+2

  • ์ด์ง„ํŠธ๋ฆฌ ์ƒ์„ฑ ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)

์ด์ง„ํŠธ๋ฆฌ ์ˆœํšŒํ•˜๊ธฐ

[1,2,3,4,5,6,7] ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ,

  • ์ „์œ„ ์ˆœํšŒ(Pre-order) : ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ (pre) ๋จผ์ € ์ˆœํšŒ๋œ๋‹ค. 1-2-4-5-3-6-7 ์ˆœ์„œ.
  • ์ค‘์œ„ ์ˆœํšŒ(In-order) : ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์ค‘๊ฐ„์ด ๋œ๋‹ค. ์™ผ์ชฝ ์ฐจ์ผ๋“œ์—์„œ ์—์„œ ๋ฆฌํ„ด ๋˜์–ด ๋‚˜์˜จ ๋‹ค์Œ์˜ ์ˆœ์„œ๊ฐ€ ๋œ๋‹ค.4-2-5-1-6-3-7 ์ˆœ์„œ.
  • ํ›„์œ„ ์ˆœํšŒ(Post-order): ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๋งˆ์ง€๋ง‰์ด ๋œ๋‹ค. ์˜ค๋ฅธ์ชฝ์—์„œ ๋ฆฌํ„ด ๋˜์–ด ๋‚˜์˜จ ๋‹ค์Œ์˜ ์ˆœ์„œ๊ฐ€ ๋œ๋‹ค. 4-5-2-6-7-3-1 ์ˆœ์„œ.

์˜ˆ์ œ ๋ชธํ’€๊ธฐ ๋ฌธ์ œ 26. ํŠธ๋ฆฌ ์ˆœํšŒ ์ฐธ๊ณ  [1,2,3,4,5] ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ,

function solution(nums) { return [ preorder(0, nums, []).join(" "), inorder(0, nums, []).join(" "), postorder(0, nums, []).join(" "), ]; } /** * * 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ ์ธ๋ฑ์Šค ์„ค์ •์‹œ ๊ธฐ์กด ์„ค์ •์—์„œ +1๋กœ ์„ธํŒ… * preorder: ๋ถ€๋ชจ๊ฐ€ pre๊ฐ€ ๋œ๋‹ค. * * @param {*} root * @param {*} nums * @param {*} result * @returns */ function preorder(root, nums, result) { if (!nums[root]) return; result.push(nums[root]); preorder(root * 2 + 1, nums, result); //left preorder(root * 2 + 2, nums, result); return result; } /** * inorder :๋ถ€๋ชจ๊ฐ€ ์ค‘๊ฐ„์ด ๋œ๋‹ค. ์™ผ์ชฝ์—์„œ ๋ฆฌํ„ด๋˜์–ด ๋‚˜์˜จ ๋‹ค์Œ์˜ ์ˆœ์„œ. * @param {*} root * @param {*} nums * @param {*} result * @returns */ function inorder(root, nums, result) { if (!nums[root]) return; inorder(root * 2 + 1, nums, result); result.push(nums[root]); inorder(root * 2 + 2, nums, result); return result; } /** * postorder: ๋ถ€๋ชจ๊ฐ€ ๋งˆ์ง€๋ง‰์ด ๋œ๋‹ค. ์˜ค๋ฅธ์ชฝ์—์„œ ๋ฆฌํ„ด๋˜์–ด ๋‚˜์˜จ ๋‹ค์Œ์˜ ์ˆœ์„œ. * @param {*} root * @param {*} nums * @param {*} result * @returns */ function postorder(root, nums, result) { if (!nums[root]) return; postorder(root * 2 + 1, nums, result); postorder(root * 2 + 2, nums, result); result.push(nums[root]); return result; } assert.deepEqual(solution([1, 2, 3, 4, 5, 6, 7]), [ "1 2 4 5 3 6 7", "4 2 5 1 6 3 7", "4 5 2 6 7 3 1", ]);

์ด์ง„ ํŠธ๋ฆฌ ํƒ์ƒ‰ํ•˜๊ธฐ

  • ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ(Binary Search Tree)๋Š” ์ด์ง„ ํŠธ๋ฆฌ(Binary Tree)์—์„œ ์›ํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํƒ์ƒ‰ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  ํŠธ๋ฆฌ ์ด๋‹ค.

์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ๊ตฌ์ถ•ํ•˜๊ธฐ

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

์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ํƒ์ƒ‰ํ•˜๊ธฐ

  • ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ํƒ์ƒ‰ ํšจ์œจ์„ ๊ฐœ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํƒ์ƒ‰ ๋Œ€์ƒ์ด ์•„๋‹Œ ๋…ธ๋“œ๋ฅผ ํ•œ๋ฒˆ์— ๋งŽ์ด ์ œ์™ธ.
  • ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ์—์„œ๋„ ์ด ๋ฐฉ๋ฒ•์ด ์ ์šฉ ๊ฐ€๋Šฅ-> ์ฐพ์œผ๋ ค๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ํ˜„์žฌ ๋…ธ๋“œ๋ณด๋‹ค ํฌ๋ฉด ์˜ค๋ฅธ์ชฝ, ์ž‘์œผ๋ฉด ์™ผ์ชฝ์„ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ์ดํ„ฐํฌ๊ธฐ์˜ ์ ˆ๋ฐ˜์„ ์ค„์ผ ์ˆ˜ ์žˆ์Œ.
  1. ์ฐพ์œผ๋ ค๋Š” ๊ฐ’์ด ํ˜„์žฌ๋…ธ๋“œ๊ฐ‘๊ณผ ๊ฐ™์œผ๋ฉด ํƒ์ƒ‰ ์ข…๋ฃŒ. ํฌ๋ฉด ์˜ค๋ฅธ์ชฝ ํƒ์ƒ‰
  2. ์ฐพ์œผ๋ ค๋Š” ๊ฐ’์ด ํ˜„์žฌ ๋…ธ๋“œ ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด ์™ผ์ชฝ ๋…ธ๋“œ ํƒ์ƒ‰.
  3. ๊ฐ’์„ ์ฐพ์œผ๋ฉด ์ข…๋ฃŒ.

์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ์‹œ๊ฐ„ ๋ณต์žก๋„

- ํŠธ๋ฆฌ ๊ท ํ˜•์— ์˜์กด - ๋…ธ๋“œ์˜ ์ฐจ์ˆ˜๊ฐ€ ๋น„์Šทํ•˜๊ฒŒ ์œ ์ง€ & ์ž์‹ ๋…ธ๋“œ์ˆ˜๊ฐ€ ๋น„์Šทํ•˜๊ฒŒ ์œ ์ง€ ๋˜๋Š” ๊ฒฝ์šฐ ๊ท ํ˜•์ด ์žกํ˜”๋‹ค๋ผ๊ณ  ๊ฐ„์ฃผ. - ๊ท ํ˜•์ด ์žกํžŒ ํŠธ๋ฆฌ : O(logN) - ์˜ˆ) ๊ท ํ˜• ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ(Balanced Binary Search Tree) - ๊ท ํ˜•์ด ๋งž์ง€ ์•Š์€ ํŠธ๋ฆฌ:O(N) - ์˜ˆ) ๋ณ€์งˆ ํŠธ๋ฆฌ( Degenerate Binary Tree) : O(N)

๋ฌธ์ œ ํ’€๊ธฐ

1. ์˜ˆ์ƒ ๋Œ€์ง„ํ‘œ (์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค )

์ด ๋ฌธ์ œ์˜ ํฌ์ธํŠธ๋Š”

[1 2] [3 4] [5 6] [7 8] [1] [2] [3] [4] [1] [2]

์ฃผ์–ด์ง„ ์ฐจ๋ก€์—์„œ 2๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฐ’์„ ํ†ตํ•ด ๋‹ค์Œ ๋Œ€์ง„ ์ˆœ์„œ๊ฐ€ ๋œ๋‹ค.

/** * * @param {*} n * @param {*} a * @param {*} b * @returns */ function solution(n, a, b) { let cnt = 0; // ์˜ฌ๋ฆผ์„ ์‚ฌ์šฉํ•˜์—ฌ 2๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฐ’์„ ๋งŒ๋“ ๋‹ค. a ์™€ b๊ฐ€ ๊ฐ™์„ ๋•Œ๊นŒ์ง€ while (a !== b) { a = Math.ceil(a / 2); //round๋„ ์ƒ๊ด€์—†์Œ b = Math.ceil(b / 2); cnt += 1; } return cnt; }

2.๋‹ค๋‹จ๊ณ„ ์นซ์†” ํŒ๋งค

๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๋Š” ๋ฌธ์ œ์˜ ํฌ์ธํŠธ๋Š” ํŒ๋งค์›์˜ ์ถ”์ฒœ์ธ๋“ค์„ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ์ด์ต์„ ๋ถ„๋ฐฐํ•˜๋Š” ๊ณผ์ •์—์„œ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์–ด๋–ค ์ž๋ฃŒ๊ตฌ์กฐ๋‚˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ๊ฒƒ์ธ๊ฐ€์™€ ์ด์ต์„ ๋ถ„๋ฐฐํ•  ๋•Œ ์ด์ต์˜ 10%๋Š” ์กฐ์ง์˜ ๊ฐ ๋ฉค๋ฒ„๊ฐ€ ๊ฐ€์ง„ ์ด์ต์—์„œ 10%๊ฐ€ ์•„๋‹Œ ์ฒ˜์Œ ํŒ๋งค์›์œผ๋กœ ๋ถ€ํ„ฐ ๋‚˜์˜จ ์ด์ต๊ธˆ์˜ 10%, ์ด ์ด์ต๊ธˆ์˜ 10%๋กœ ์ด์ต๊ธˆ์„ ๋ถ„๋ฐฐํ•œ๋‹ค.

ํŠธ๋ฆฌ๊ตฌ์กฐ๋Š” ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ถ”์ฒœ์ธ์„ ๋”ฐ๋ผ๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ O(1)์˜ ๋ณต์žก๋„๋กœ ๊ตฌํ˜„ํ•˜๋Š”๊ฑด ์–ด๋ ต์ง€ ์•Š์•˜๋‹ค. ๋ฌธ์ œ๋Š” ์ถ”์ฒœ์ธ์ด ์žˆ์„๋•Œ while๋ฌธ์œผ๋กœ ์ถ”์ฒœ์ธ์ด ์—†์„ ๋•Œ๊นŒ์ง€ ์ด์ต์„ ๋ถ„๋ฐฐํ•˜๋Š”๊ฒƒ์ธ๋ฐ, ์ด์ต ๋ถ„๋ฐฐ ๊ณ„์‚ฐ์„ ํ˜„์žฌ ์ถ”์ฒœ์ธ์ด ๋ฐ›์„ ์ด์ต์˜ 10%๋ฅผ ๊ณ„์‚ฐ, ํŒ๋งค์ž๋Š” 10%๋ฅผ ๋บ€ ๊ธˆ์•ก์˜ ์ด์ต์„ ๊ณ„์‚ฐํ•ด์•ผ ํ•œ๋‹ค.

๋ฌธ์ œ์—์„œ ์–ธ๊ธ‰ํ•œ ์ด์ต์ด 1๋ณด๋‹ค ์ž‘์€๊ฒฝ์šฐ๋Š” ๋ถ„๋ฐฐํ•˜์ง€ ์•Š๋Š”๋‹ค ๋ฅผ while๋ฌธ์˜ ์กฐ๊ฑด์œผ๋กœ ์ˆœํšŒ๋ฅผ ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ํ•ด์•ผ ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ๋ฉด ํ• ์ˆ˜ ์žˆ๋‹ค.

function solution(enroll, referral, seller, amount) { const adjacentList = new Map(); enroll.forEach((seller, i) => { adjacentList.set(seller, { referral: referral[i], profit: 0 }); }); for( const idx in seller){ let initialAmount = amount[idx] * 100; let referral = adjacentList.get(seller[idx]); let profit = 0; while( initialAmount>=1 && referral){ profit = Math.floor(initialAmount*0.1) //10% referral.profit+= initialAmount-profit //10% initialAmount = profit referral = adjacentList.get(referral.referral); } } return enroll.map(seller => adjacentList.get(seller).profit); }

3.๋ฏธ๋กœํƒˆ์ถœ

4. ์–‘๊ณผ๋Š‘๋Œ€

์ด ๋ฌธ์ œ๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ bfs์—์„œ ์–‘์ด ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ ์žˆ๋Š” ์–‘๊นŒ์ง€ ๊ตฌํ–ˆ์„ ๋•Œ max๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋„ˆ๋ฌด ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•œ๊ฒŒ ๋ฌธ์ œ๋‹ค. ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ๋กœ์˜ ๊ธธ์„ ๊ฐ”์„ ๋•Œ ์žก์•„ ๋จนํžˆ์ง€ ์•Š๋Š” ์ƒํƒœ์—์„œ ์ตœ๋Œ€ ์–‘์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

bfs๋กœ ํ’€์—ˆ์„ ๊ฒฝ์šฐ, 0๋ฒˆ ์–‘์—์„œ ์ธ์ ‘ํ•œ 1,8๋ฒˆ ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š”๋ฐ 1๋ฒˆ์œผ๋กœ ๊ฐ€๊ฒŒ๋˜๋ฉด ์–‘์ด 2๊ฐœ ์ด๋ฏ€๋กœ ๊ณ„์†ํ•ด์„œ ๋” ๋‚˜์•„๊ฐ€๊ณ , 8๋ฒˆ ๋…ธ๋“œ์˜ ๊ฒฝ์šฐ ์–‘1, ๋Š‘๋Œ€1๋กœ ์–‘์ด ์žก์•„๋จนํžˆ๋ฏ€๋กœ ๋‚ด ํ’€์ด์—์„œ๋Š” 8์„ ๋‹ค์‹œ๋Š” ๊ฐ€์ง€์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ 8์„ ๊ผญ 0๋‹ค์Œ์— ๊ฐ€์•ผ ํ•˜๋Š”๊ฑด ์•„๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๊ณณ์„ ๋“ค๋ ธ๋‹ค๊ฐ€ ์ตœ๋Œ€ํ•œ ์–‘์„ ๋ชจ์•„ ๋‹ค์‹œ 8๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ถ€๋ถ„์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„  ํ์— ๋“ค์–ด๊ฐ€๋Š” ๋งค ๋…ธ๋“œ๋งˆ๋‹ค ์กฐ๊ฑด์— ์˜ํ•ด ๋ฐฉ๋ฌธ ๋ชปํ•œ 8์„ ๊ณ„์† ์œผ๋กœ ๋“ค๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

โŒ ์ž˜๋ชป๋œ ํ’€์ด

function solution(info, edges) { let queue = []; let adjacentList = {} for(const [from, to] of edges){ adjacentList[from] = adjacentList[from] ||[] adjacentList[from].push(to) } let max = 0 queue= [{node:0 , curShip:1, curWolf:0 }] while(queue.length){ let {node, curShip, curWolf }= queue.shift(); max = Math.max(max, curShip) let nextNodes = adjacentList[node]; // console.log(queue,node, curShip,curWolf,nextNodes) if(!nextNodes) continue for( const nextNode of nextNodes){ let nextShip= info[nextNode] ===0? curShip+1 : curShip let nextWolf = info[nextNode]===1?curWolf+1: curWolf if(nextShip>nextWolf){ queue.push({node:nextNode, curShip: nextShip, curWolf: nextWolf }) } } } return max }

โœ… ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๋ฅผ ๋งค ํ์— ๋„ฃ์–ด์ฃผ๋Š” ์ฝ”๋“œ

function solution(info, edges) { let queue = []; let adjacentList = {} for(const [from, to] of edges){ adjacentList[from] = adjacentList[from] ||[] adjacentList[from].push(to) } let max = 0 queue= [{node:0 , curShip:1, curWolf:0, availableNodes:[] }] while(queue.length){ let {node, curShip, curWolf,availableNodes }= queue.shift(); max = Math.max(max, curShip) let nextNodes=adjacentList[node] if(nextNodes) availableNodes=[...availableNodes, ...nextNodes] for( const nextNode of availableNodes){ if(info[nextNode]){ if(curShip !== curWolf+1){ let n = [...availableNodes] let removeItem = n.indexOf(nextNode) n.splice(removeItem, 1); queue.push({node:nextNode, curShip, curWolf:curWolf+1,availableNodes:n }) } }else{ let n = [...availableNodes] let removeItem = n.indexOf(nextNode) n.splice(removeItem, 1); queue.push({node:nextNode, curShip:curShip+1, curWolf, availableNodes:n }) } } } return max }

5.๊ธธ ์ฐพ๊ธฐ ๊ฒŒ์ž„

Hi, I'm Hyunsu ๐Ÿ‘‹

Profile Image

์•ˆ๋…•ํ•˜์„ธ์š”. ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ฃผํ˜„์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ฐœ๋ฐœ์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ํ’๋ถ€ํ•˜๊ณ  ๊ฐ€์น˜ ์žˆ๋Š” ๊ฒฝํ—˜์„ ์ œ๊ณตํ•˜๋Š” ์ผ์— ๋ฟŒ๋“ฏํ•จ์„ ๋Š๋‚๋‹ˆ๋‹ค.

์˜ต์‹œ๋””์–ธ(Obsidian)์—์„œ ํ˜„์žฌ ๋ธ”๋กœ๊ทธ๋กœ ํ•˜๋‚˜์”ฉ ๊ธ€์„ ์˜ฎ๊ธฐ๋Š” ๊ณผ์ •์— ์žˆ์–ด์š”. โ˜•๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป โ›ท

Github on ViewReach Me Out