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

๐Ÿ–ฅ dev-ruby

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ‘œ ํŽธ์ง‘ | ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ | 2021 ์นด์นด์˜ค ์ฑ„์šฉ์—ฐ๊ณ„ํ˜• ์ธํ„ด์‹ญ | Lv.3 ๋ณธ๋ฌธ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ‘œ ํŽธ์ง‘ | ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ | 2021 ์นด์นด์˜ค ์ฑ„์šฉ์—ฐ๊ณ„ํ˜• ์ธํ„ด์‹ญ | Lv.3

ruby_s 2022. 5. 4. 09:34
728x90
๋ฐ˜์‘ํ˜•
SMALL

๋ฌธ์ œ

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ‘œ ํŽธ์ง‘

8 2 ["D 2","C","U 3","C","D 4","C","U 2","Z","Z"] "OOOOXOOO" 8 2 ["D 2","C","U 3","C","D 4","C","U 2","Z","Z","U 1","C"] "OOXOXOOO"

programmers.co.kr

 

ํ’€์ด

function solution(n, k, cmd) {
  const stack = [];
  const answer = Array.from({ length: n }, () => "O");
  const list = Array.from({ length: n }, (_, index) => [index - 1, index + 1]);
  list[n - 1][1] = -1;

  function upNode(k, row) {
    for (let i = 0; i < row; i++) k = list[k][0];
    return k;
  }

  function downNode(k, row) {
    for (let i = 0; i < row; i++) k = list[k][1];
    return k;
  }

  function deleteNode(k) {
    const [prev, next] = list[k];
    stack.push([k, prev, next]);
    answer[k] = "X";

    if (next === -1) {
      if (prev !== -1) list[prev][1] = next;
      k = prev;
    } else {
      list[next][0] = prev;
      if (prev !== -1) list[prev][1] = next;
      k = next;
    }
    return k;
  }

  function restoreNode() {
    const [node, prev, next] = stack.pop();
    if (prev !== -1) list[prev][1] = node;
    if (next !== -1) list[next][0] = node;
    answer[node] = "O";
  }

  for (const item of cmd) {
    const [direct, row] = item.split(" ");
    switch (direct) {
      case "D":
        k = downNode(k, +row);
        break;
      case "U":
        k = upNode(k, +row);
        break;
      case "C":
        k = deleteNode(k);
        break;
      case "Z":
        restoreNode();
        break;
    }
  }
  return answer.join("");
}

์ด ๋ฌธ์ œ๋Š” ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํšจ์œจ์„ฑ๊นŒ์ง€ ํ†ต๊ณผํ•˜๋Š” ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค.. 

function upNode(k, row) {
    for (let i = 0; i < row; i++) k = list[k][0];
    return k;
}

k์˜ ์œ„์น˜๋ฅผ ์œ„๋กœ ์˜ฌ๋ฆฌ๋Š” ํ•จ์ˆ˜๋ถ€ํ„ฐ ๋ณด์ž. row์—๋Š” ์–ผ๋งŒํผ ์˜ฌ๋ ค์•ผ ํ•˜๋Š” ์ง€ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์˜จ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด 3๋งŒํผ ์˜ฌ๋ฆฌ๋ ค๋ฉด, 3๋ฒˆ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ํ˜„์žฌ k์œ„์น˜๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” prev๋ฅผ k์— ๋Œ€์ž…ํ•ด์ค€๋‹ค. ๊ทธ๋ ‡๊ฒŒ 3๋ฒˆ ๋ฐ˜๋ณตํ•˜๋ฉด k์˜ ํ˜„์žฌ ์œ„์น˜๋Š” 3๋ฒˆ์งธ ์œ„์— ์žˆ๋Š” ๋…ธ๋“œ๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. downNode ํ•จ์ˆ˜๋„ ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์ด๋‹ค.
์ค‘์š”ํ•œ ๊ฒƒ์€ deleteNode์™€ restoreNode ํ•จ์ˆ˜์ด๋‹ค.

function deleteNode(k) {
    const [prev, next] = list[k];
    stack.push([k, prev, next]);
    answer[k] = "X";

    if (next === -1) {
      if (prev !== -1) list[prev][1] = next;
      k = prev;
    } else {
      list[next][0] = prev;
      if (prev !== -1) list[prev][1] = next;
      k = next;
    }
    return k;
}

next๊ฐ€ 1์ธ ๊ฒฝ์šฐ๋Š” ๋งˆ์ง€๋ง‰ ํ–‰์„ ๋œปํ•˜๋ฏ€๋กœ k๋ฅผ ์ด์ „ ํ–‰์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  prev๊ฐ€ -1์ด ์•„๋‹Œ ๊ฒฝ์šฐ (ํ–‰์ด ํ•œ๊ฐœ์ธ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ), ์ด์ „ ํ–‰์˜ next๋Š” -1์ด ๋œ๋‹ค.

next๊ฐ€ 1์ด ์•„๋‹Œ ๊ฒฝ์šฐ๋Š” ๋งˆ์ง€๋ง‰ ํ–‰์ด ์•„๋‹ ๋•Œ์ด๋‹ค. next๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” prev๋ฅผ ํ˜„์žฌ์˜ prev๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๊ณ , prev์˜ ๋‹ค์Œ ๊ฐ’์ด ํ˜„์žฌ์˜ next๊ฐ€ ๋˜๋„๋ก ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  k๋ฅผ ๋‹ค์Œ ํ–‰์œผ๋กœ ๋„˜๊ธด๋‹ค.

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

function restoreNode() {
    const [node, prev, next] = stack.pop();
    if (prev !== -1) list[prev][1] = node;
    if (next !== -1) list[next][0] = node;
    answer[node] = "O";
}

๊ฐ€์žฅ ์ตœ์‹ ์˜ ํ–‰์„ ์Šคํƒ์—์„œ popํ•œ๋‹ค. if์œผ๋กœ ๋ชจ๋‘ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ณ , prev์˜ ๋‹ค์Œ ๊ฐ’์„ ํ˜„์žฌ ํ–‰์œผ๋กœ, next์˜ ์ด์ „ ๊ฐ’์„ ํ˜„์žฌ ํ–‰์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  answer์˜ node์œ„์น˜๋ฅผ O๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค !

 

์ž๋ฃŒ๊ตฌ์กฐ ๋•Œ ๋ฐฐ์› ๋˜ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ณต์Šตํ–ˆ๋‹ค..๐Ÿ™ƒ
728x90
๋ฐ˜์‘ํ˜•
LIST