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

๐Ÿ–ฅ dev-ruby

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ธธ ์ฐพ๊ธฐ ๊ฒŒ์ž„ | ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(js) | Lv.3 | ์ด์ง„ํŠธ๋ฆฌ | 2019 KAKAO BLIND RECRUITMENT ๋ณธ๋ฌธ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ธธ ์ฐพ๊ธฐ ๊ฒŒ์ž„ | ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(js) | Lv.3 | ์ด์ง„ํŠธ๋ฆฌ | 2019 KAKAO BLIND RECRUITMENT

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

๋ฌธ์ œ

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ธธ ์ฐพ๊ธฐ ๊ฒŒ์ž„

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

programmers.co.kr

 

ํ’€์ด

function solution(nodeinfo) {
  const sorted = nodeinfo
    .map((el, index) => [...el, index + 1])
    .sort((a, b) => (a[1] === b[1] ? a[0] - b[0] : b[1] - a[1]));

  function order(sorted, preAnswer, postAnswer) {
    const [x, y, node] = sorted[0];
    const leftChild = [];
    const rightChild = [];

    for (let i = 1; i < sorted.length; i++) {
      const row = sorted[i];
      if (row[0] < x) leftChild.push(row);
      else rightChild.push(row);
    }

    preAnswer.push(node);
    if (leftChild.length) order(leftChild, preAnswer, postAnswer);
    if (rightChild.length) order(rightChild, preAnswer, postAnswer);
    postAnswer.push(node);
  }

  const preAnswer = [];
  const postAnswer = [];
  order(sorted, preAnswer, postAnswer);
  return [preAnswer, postAnswer];
}

์ด ๋ฌธ์ œ๋Š” ๊ฐ ๋…ธ๋“œ๋งˆ๋‹ค x์™€ y์ขŒํ‘œ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด์ง„ํŠธ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž์‹ ๋…ธ๋“œ๋Š” ์ตœ๋Œ€ 2๊ฐœ๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ๋ฌด์ž‘์ • Y๊ฐ’์ด ํ˜„์žฌ ๋…ธ๋“œ๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์„ ํƒ์ƒ‰ํ•˜๊ณ  ๊ทธ ์ค‘ X๊ฐ’์ด ์ž‘์œผ๋ฉด ์™ผ์ชฝ์— ๋ฐฐ์น˜, X๊ฐ’์ด ํฌ๋ฉด ์˜ค๋ฅธ์ชฝ์— ๋ฐฐ์น˜ ํ•˜๋ ค๊ณ  ํ–ˆ์œผ๋‚˜..! ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
2๋…ธ๋“œ์˜ ์™ผ์ชฝ ์•„๋ž˜์— 1์ด ์กด์žฌํ•˜์ง€๋งŒ, 1์€ 2์˜ ์ž์‹๋…ธ๋“œ๋Š” ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹จ์ˆœํžˆ x, y์ขŒํ‘œ๋กœ๋งŒ ํŒ๋‹จํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค..

const sorted = nodeinfo
  .map((el, index) => [...el, index + 1])
  .sort((a, b) => (a[1] === b[1] ? a[0] - b[0] : b[1] - a[1]));

๋จผ์ € y๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ๊ทธ ์ „์— ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ์–ตํ•  ํ•„์š”๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋งˆ์ง€๋ง‰ ์›์†Œ์— index๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. ์™ผ์ชฝ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— y๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด x๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค.

์ด์ œ ์ˆœํšŒ ํ•จ์ˆ˜๋ฅผ ๋ณด์ž.

function order(sorted, preAnswer, postAnswer) {
   const [x, y, node] = sorted[0];
   const leftChild = [];
   const rightChild = [];

   for (let i = 1; i < sorted.length; i++) {
      const row = sorted[i];
      if (row[0] < x) leftChild.push(row);
      else rightChild.push(row);
   }

   preAnswer.push(node);
   if (leftChild.length) order(leftChild, preAnswer, postAnswer);
   if (rightChild.length) order(rightChild, preAnswer, postAnswer);
   postAnswer.push(node)
}

sorted์˜ ์ƒ์œ„๋…ธ๋“œ๋ฅผ ๊บผ๋‚ด์ค€๋‹ค. ํ•ด๋‹น ์ƒ์œ„ ๋…ธ๋“œ๋Š” answer์— ํ•˜๋‚˜์”ฉ pushํ•  node์ด๋‹ค.
์ˆœํšŒ๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— answer์— pushํ•˜๋ฉด ์ „์œ„์ˆœํšŒ, ์ˆœํœ˜๊ฐ€ ๋๋‚œ ํ›„ ๋’ค์—์„œ๋ถ€ํ„ฐ pushํ•˜๋ฉด ํ›„์œ„์ˆœํšŒ๊ฐ€ ๋œ๋‹ค.

์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•ด์•ผ ํ•  ๊ฒƒ์€ ๋ฐ˜๋ณต๋ฌธ์˜ i๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฏธ ์ •๋ ฌ์ด ๋ผ์žˆ๋Š” ๋ฐฐ์—ด์ด๋ฏ€๋กœ, ์ƒ์œ„๋…ธ๋“œ์˜ ์•„๋ž˜ ๋…ธ๋“œ๋“ค์„ ์ญ‰ ํƒ์ƒ‰ํ•œ๋‹ค.
์ƒ์œ„๋…ธ๋“œ์˜ x๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์™ผ์ชฝ ์„œ๋ธŒ ํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋…ธ๋“œ์ด๋ฏ€๋กœ leftChild์— ๋„ฃ์–ด์ฃผ๊ณ , ์ƒ์œ„๋…ธ๋“œ์˜ x๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋…ธ๋“œ์ด๋ฏ€๋กœ rightChild์— ๋„ฃ์–ด์ค€๋‹ค.

preAnswer์—๋Š” ์ƒ์œ„๋…ธ๋“œ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅํ•˜๋ฉด ๋˜๊ณ , ์™ผ์ชฝ ์ž์‹๊ณผ ์˜ค๋ฅธ์ชฝ ์ž์‹์ด ์žˆ์„ ๊ฒฝ์šฐ, ๊ฐ๊ฐ ์™ผ์ชฝ ์ˆœํšŒ์™€ ์˜ค๋ฅธ์ชฝ ์ˆœํšŒ๋ฅผ ๋Œ๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ ์™ผ์ชฝ ์ž์‹(leftChild)๊ณผ ์˜ค๋ฅธ์ชฝ ์ž์‹(rightChild)์ด ๋ชจ๋‘ ์—†๋‹ค๋ฉด ์ˆœํšŒ๋Š” ๋๋‚ฌ๋‹ค. ์ด์ œ ๋งจ ํ•˜๋‹จ์˜ ๋…ธ๋“œ๋ถ€ํ„ฐ ์žฌ๊ท€๋ฅผ ๋๋‚ด๋ฉด์„œ postAnswer์— ์ €์žฅ๋  ๊ฒƒ์ด๋‹ค.

์‚ฌ์‹ค ํŠธ๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐฐ์—ด๋กœ ๊ตฌ์„ฑํ•ด์•ผ ํ• ๊นŒ ์—„์ฒญ ๋งŽ์€ ๊ณ ๋ฏผ์„ ํ–ˆ๋‹ค. ๊ทธ๋Ÿด ํ•„์š”์—†์ด ์ •๋ ฌ์„ ํ•˜๋ฉด ๋˜๋Š”๋ฐ ๋Œ€์‹  ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ์–ตํ•ด์•ผ ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค๊นŒ์ง€ ์Šค์Šค๋กœ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ์ง€๋งŒ..! ์ˆœํšŒ๋ฅผ ๊นŒ๋จน์–ด์„œ ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค

 

728x90
๋ฐ˜์‘ํ˜•
LIST