notion-029

๐Ÿ˜‰ ํ—ท๊ฐˆ๋ ธ๋˜ & ๋ชฐ๋ž๋˜ ๋ถ€๋ถ„๋“ค๋งŒ ์ •๋ฆฌํ•˜๋Š” ๋‚˜๋งŒ์˜ TIL
๐Ÿ˜ฏ ๋ชจ๋“  ๊ฐ•์˜ ๋‚ด์šฉ์€ ์ ์ง€ ์•Š์•„์š”!

์˜ค๋Š˜์˜ ์†Œ๊ฐ์€?
์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ์—ฐ์Šต์„ ์กฐ๊ธˆ ๋Šฆ๊ฒŒ ์‹œ์ž‘ํ•œ ํŽธ์ด๋ผ ์•„์ง ๋ฐฑ์ค€ 100๋ฌธ์ œ๋„ ๋‹ค ๋ชปํ’€์—ˆ๋‹ค.
๊ทธ๋ž˜์„œ ์ด์ง„ํŠธ๋ฆฌ๋Š” ์ณ๋‹ค๋ณด์ง€๋„ ๋ชปํ•  ๊ตฌ์—ญ์ด์—ˆ๋‹ค.. ^^...

์‚ญ์ œ ๋ฉ”์†Œ๋“œ ์งœ๊ณ  ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธํ•˜๋Š”๋ฐ์—๋งŒ ์˜ค๋Š˜ ์‹œ๊ฐ„ ๋‹ค์ผ๋‹ค.
์•„์ง ์ด์ง„ํŠธ๋ฆฌ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ•ด์„œ ๋” ๊ณต๋ถ€ํ•ด๋ด์•ผ๊ฒ ๋‹ค.




[+] ์‹ค์Šต - ๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ

๐Ÿ˜‚ ์™œ ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚˜๋ƒ๊ณ  ..

๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ๋ฅผ ๋”ฑ ๋ณด๋ฉด ์ƒ๊ฐ๋‚˜๋Š” ๋กœ์ง์ด BFS์˜€์Šต๋‹ˆ๋‹ค.
์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ํ’€์—ˆ์–ด์•ผ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ์•ˆ๋‚ฌ์„๊นŒ,,? ์•„์ง JS๋กœ ์ฝ”ํ…Œ๋Š” ๋ฌด๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”..


  • ์ดˆ๊ธฐ ๋‹ต์•ˆ
function popVal(arr) {
  const val = arr[0];

  for (let i = 0; i < arr.length - 1; i += 1) {
    arr[i] = arr[i + 1];
  }
  arr.splice(-1, 1);

  return val;
}

function bfs(s, arr, e) {
  const visit = new Array(e + 1).fill(false);
  const val = new Array(e + 1).fill(0);
  const queue = [s];
  visit[s] = true;

  while (queue.length) {
    const num = popVal(queue);
    const v = val[num] + 1;

    for (let node of arr) {
      if (node[0] === num && !visit[node[1]]) {
        queue.push(node[1]);
        visit[node[1]] = true;
        val[node[1]] = v;
      } else if ((node[1] === num) & !visit[node[0]]) {
        queue.push(node[0]);
        visit[node[0]] = true;
        val[node[0]] = v;
      }
    }
  }

  const maxVal = Math.max.apply(null, val);
  return val.filter((i) => i === maxVal).length;
}

function solution(n, edge) {
  return bfs(1, edge, n);
}

Untitled


๐Ÿ˜ฃ ํŒŒ์ด์ฌ์œผ๋กœ ์งœ๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ ์ฝ”๋“œ๋กœ ์ˆ˜์ •ํ•ด๋ดค์Šต๋‹ˆ๋‹ค.


  • ์ˆ˜์ • ๋‹ต์•ˆ
function popVal(arr) {
  const val = arr[0];

  for (let i = 0; i < arr.length - 1; i += 1) {
    arr[i] = arr[i + 1];
  }
  arr.splice(-1, 1);

  return val;
}

function bfs(s, graph, e) {
  const visit = new Array(e + 1).fill(false);
  const val = new Array(e + 1).fill(0);
  const queue = [s];
  visit[s] = true;

  while (queue.length) {
    const num = popVal(queue);
    const v = val[num] + 1;

    for (let n of graph[num]) {
      if (!visit[n]) {
        queue.push(n);
        visit[n] = true;
        val[n] = v;
      }
    }
  }

  const maxVal = Math.max.apply(null, val);

  return val.filter((i) => i === maxVal).length;
}

function solution(n, edge) {
  const graph = Array.from(Array(n + 1), () => []);

  for (const [a, b] of edge) {
    graph[a].push(b);
    graph[b].push(a);
  }

  return bfs(1, graph, n);
}

Untitled 1

๐Ÿ˜ฎ ์„ฑ๋Šฅ์ด ์ด๋ ‡๊ฒŒ๋‚˜ ์ฐจ์ด๊ฐ€ ๋‚œ๋‹ค๋‹ˆ..
์ด์ œ ํ™•์‹คํžˆ JS๋กœ ์ฝ”๋“œ ์งœ๋Š”๊ฒŒ ๊ฐ์ด ์˜ค๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.




[1] ํŠธ๋ฆฌ(Tree)

ํŠธ๋ฆฌ๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์˜ ์ผ์ข…์œผ๋กœ ์ •์ ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ„์„ ์ด ํ•˜๋‚˜ ๋ฐ–์— ์—†๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


[1-1] ํŠธ๋ฆฌ ํŠน์ง•

  • ๋ฃจํŠธ ์ •์ ์„ ์ œ์™ธํ•œ ๋ชจ๋“  ์ •์ ์€ ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ ๋ถ€๋ชจ ์ •์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  • ์ •์ ์ด N๊ฐœ์ธ ํŠธ๋ฆฌ๋Š” ๋ฐ˜๋“œ์‹œ N-1๊ฐœ์˜ ๊ฐ„์„ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  • ๋ฃจํŠธ์—์„œ ํŠน์ • ์ •์ ์œผ๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๋Š” ์œ ์ผํ•ฉ๋‹ˆ๋‹ค.


[1-2] ์ด์ง„ ํŠธ๋ฆฌ

์ด์ง„ ํŠธ๋ฆฌ๋Š” ๊ฐ ์ •์ ์ด ์ตœ๋Œ€ 2๊ฐœ์˜ ์ž์‹์„ ๊ฐ€์ง€๋Š” ํŠธ๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ด์ง„ ํŠธ๋ฆฌ, ์™„์ „ ์ด์ง„ํŠธ๋ฆฌ, ํฌํ™” ์ด์ง„ํŠธ๋ฆฌ ์ด ์™ธ์—๋Š” ํŽธํ–ฅ ํŠธ๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.


ํŠน์ง•

  • ์ •์ ์ด N๊ฐœ์ธ ์ด์ง„ ํŠธ๋ฆฌ๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋†’์ด๊ฐ€ N์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ •์ ์ด N๊ฐœ์ธ ํฌํ™” ๋˜๋Š” ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ ๋†’์ด๋Š” log N ์ž…๋‹ˆ๋‹ค.
  • ๋†’์ด๊ฐ€ h์ธ ํฌํ™” ์ด์ง„ ํŠธ๋ฆฌ๋Š” 2^h-1๊ฐœ์˜ ์ •์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  • ์ผ๋ฐ˜์ ์ธ ์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋งŽ์ง€ ์•Š๊ณ , ๋‹ค์Œ ์ž๋ฃŒ๊ตฌ์กฐ์— ์‘์šฉ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค.
    โ†’ ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ, ํž™, AVL ํŠธ๋ฆฌ, ๋ ˆ๋“œ ๋ธ”๋ž™ ํŠธ๋ฆฌ


[1-3] ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

ํŠธ๋ฆฌ๋Š” ์ธ์ ‘ ํ–‰๋ ฌ, ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


  • ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„

์ธ๋ฑ์Šค * 2 ๋Š” ์™ผ์ชฝ ์ •์ , ์ธ๋ฑ์Šค * 2 + 1์€ ์˜ค๋ฅธ์ชฝ ์ •์ ์ด ๋ฉ๋‹ˆ๋‹ค.

๋ถ€๋ชจ ์ •์ ์„ ์•Œ๊ณ ์‹ถ๋‹ค๋ฉด, ์ธ๋ฑ์Šค // 2 (์†Œ์ˆซ์ ์„ ๋ฒ„๋ฆฐ ์ƒํƒœ)๋กœ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const tree = [
  undefined,
  9,
  3,
  8,
  2,
  5,
  undefined,
  7,
  undefined,
  undefined,
  undefined,
  4,
];


  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„
class Node {
	constructor() {
		this.val = val;
		this.left = null;
		this.right = null;
	}
}

class Tree {
	constructor() {
		this.root = node;
	}

	//... methods : queue๋ฅผ ์„ ์–ธํ•ด์„œ enqueue๋กœ left, right ์ž์‹ ๋…ธ๋“œ๋“ค์„ ๋”ํ•ด์ค€๋‹ค.
}

const tree = new Tree(new Node(10));
tree.root.left = new Node(5);
tree.root.right = new Node(9);
tree.root.left.left = new Node(3);
...




[2] ํž™(Heap)

์ด์ง„ ํŠธ๋ฆฌ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋ฉฐ,
์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์š”์†Œ๊ฐ€ ๋จผ์ € ๋‚˜๊ฐ€๊ธฐ ์œ„ํ•ด ์š”์†Œ๊ฐ€ ์‚ฝ์ž…, ์‚ญ์ œ๋  ๋•Œ ๋ฐ”๋กœ ์ •๋ ฌ๋˜๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํž™์€ โ€˜์šฐ์„ ์ˆœ์œ„ ํโ€™๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ์— ๊ฐ€์žฅ ์ ํ•ฉํ•œ ์š”์†Œ์ž…๋‹ˆ๋‹ค.


์šฐ์„ ์ˆœ์œ„ ํ
FIFO์ธ ํ์™€ ๋‹ฌ๋ฆฌ ์šฐ์„  ์ˆœ์œ„๊ฐ€ ๋†’์€ ์š”์†Œ๊ฐ€ ๋จผ์ € ๋‚˜๊ฐ€๋Š” ํ


[2-1] ํž™์˜ ํŠน์ง•

  • ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์š”์†Œ๊ฐ€ ๋จผ์ € ๋‚˜๊ฐ€๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฃจํŠธ๊ฐ€ ๊ฐ€์žฅ ํฐ ๊ฐ’์ด ๋˜๋Š” ์ตœ๋Œ€ ํž™(Max Heap)๊ณผ ๋ฃจํŠธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์ด ๋˜๋Š” ์ตœ์†Œ ํž™(Min Heap)์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • JS์—์„œ๋Š” ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.)


[2-2] ํž™ ์•Œ๊ณ ๋ฆฌ์ฆ˜

(1) ์š”์†Œ ์ถ”๊ฐ€

  • ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ๋Š” ํŠธ๋ฆฌ์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ •์ ์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์ถ”๊ฐ€ ํ›„ ๋ถ€๋ชจ ์ •์ ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์œผ๋ฉด ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
  • ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉด ๊ฒฐ๊ตญ ๊ฐ€์žฅ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์ •์ ์ด ๋ฃจํŠธ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.


(2) ์š”์†Œ ์ œ๊ฑฐ

  • ์š”์†Œ ์ œ๊ฑฐ๋Š” ๋ฃจํŠธ ์ •์ ๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฃจํŠธ ์ •์ ์ด ์ œ๊ฑฐ๋œ ํ›„ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ •์ ์ด ๋ฃจํŠธ์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฃจํŠธ ์ •์ ์˜ ๋‘ ์ž์‹ ์ •์  ์ค‘ ๋” ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์ •์ ๊ณผ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
  • ๋‘ ์ž์‹ ์ •์ ์ด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋” ๋‚ฎ์„ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค,

์™„์ „ ์ด์ง„ํŠธ๋ฆฌ์˜ ๋†’์ด๋Š” log N ์ด๊ธฐ ๋•Œ๋ฌธ์—, ํž™์˜ ์š”์†Œ ์ถ”๊ฐ€/์ œ๊ฑฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ O(log N)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.


[2-3] ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

  • MaxHeap
class MaxHeap {
  constructor() {
    this.heap = [null];
  }

  push(val) {
    this.heap.push(val);
    let curIdx = this.heap.length - 1;
    let parIdx = Math.floor(curIdx / 2);

    while (parIdx !== 0 && this.heap[parIdx] < val) {
      const tmp = this.heap[parIdx];
      this.heap[parIdx] = val;
      this.heap[curIdx] = tmp;

      curIdx = parIdx;
      parIdx = Math.floor(curIdx / 2);
    }
  }

  pop() {
    const returnVal = this.heap[1];
    this.heap[1] = this.heap.pop();

    let curIdx = 1;
    let leftIdx = 2;
    let rightIdx = 3;

    while (
      this.heap[curIdx] < this.heap[leftIdx] ||
      this.heap[curIdx] < this.heap[rightIdx]
    ) {
      if (this.heap[leftIdx] < this.heap[rightIdx]) {
        const tmp = this.heap[curIdx];
        this.heap[curIdx] = this.heap[rightIdx];
        this.heap[rightIdx] = tmp;
        curIdx = rightIdx;
      } else {
        const tmp = this.heap[curIdx];
        this.heap[curIdx] = this.heap[leftIdx];
        this.heap[leftIdx] = tmp;
        curIdx = leftIdx;
      }
      leftIdx = curIdx * 2;
      rightIdx = curIdx * 2 + 1;
    }

    return returnVal;
  }
}

const heap = new MaxHeap();
heap.push(45);
heap.push(30);
console.log(heap.heap);
heap.push(70);
console.log(heap.heap);
heap.push(43);
console.log(heap.heap);

const popArr = [];
popArr.push(heap.pop());
console.log(popArr);
console.log(heap.heap);

Untitled 2




[+] ๊ณผ์ œ - MinHeap

๊ฐ„๋‹จํ•˜๊ฒŒ MaxHeap์˜ > ์กฐ๊ฑด์„ <๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋˜๋Š” ๊ณผ์ œ์˜€์Šต๋‹ˆ๋‹ค.
MaxHeap, MinHeap์˜ ์ฐจ์ด๋ฅผ ์•Œ๊ณ  ์›๋ฆฌ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๋‚ด์ฃผ์‹  ๊ณผ์ œ๊ฐ™์•„์š”.

  • MinHeap
class MinHeap {
  constructor() {
    this.heap = [null];
  }

  push(val) {
    this.heap.push(val);
    let curIdx = this.heap.length - 1;
    let parIdx = Math.floor(curIdx / 2);

    while (parIdx !== 0 && this.heap[parIdx] > val) {
      const tmp = this.heap[parIdx];
      this.heap[parIdx] = val;
      this.heap[curIdx] = tmp;

      curIdx = parIdx;
      parIdx = Math.floor(curIdx / 2);
    }
  }

  pop() {
    const returnVal = this.heap[1];
    this.heap[1] = this.heap.pop();

    let curIdx = 1;
    let leftIdx = 2;
    let rightIdx = 3;

    while (
      this.heap[curIdx] > this.heap[leftIdx] ||
      this.heap[curIdx] > this.heap[rightIdx]
    ) {
      if (this.heap[leftIdx] > this.heap[rightIdx]) {
        const tmp = this.heap[curIdx];
        this.heap[curIdx] = this.heap[rightIdx];
        this.heap[rightIdx] = tmp;
        curIdx = rightIdx;
      } else {
        const tmp = this.heap[curIdx];
        this.heap[curIdx] = this.heap[leftIdx];
        this.heap[leftIdx] = tmp;
        curIdx = leftIdx;
      }
      leftIdx = curIdx * 2;
      rightIdx = curIdx * 2 + 1;
    }

    return returnVal;
  }
}

const heap = new MinHeap();
heap.push(45);
heap.push(30);
console.log(heap.heap);
heap.push(70);
console.log(heap.heap);
heap.push(43);
console.log(heap.heap);

const popArr = [];
popArr.push(heap.pop());
console.log(popArr);
console.log(heap.heap);

Untitled 3

์ œ์ผ ์ž‘์€ ๊ฐ’์ด root์— ๋ฐฐ์น˜๋˜๋Š” MinHeap๊ฐ€ ์ž˜ ์ž‘๋™ํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.




[3] ํŠธ๋ผ์ด(Trie)

ํŠธ๋ผ์ด๋Š” ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ํšจ์œจ์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ํŠธ๋ฆฌ ํ˜•ํƒœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.


[3-1] ํŠธ๋ผ์ด์˜ ํŠน์ง•

  • ๊ฒ€์ƒ‰์–ด ์ž๋™์™„์„ฑ, ์‚ฌ์ „ ์ฐพ๊ธฐ ๋“ฑ์— ์‘์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฌธ์ž์—ด์„ ํƒ์ƒ‰ํ•  ๋•Œ ๋‹จ์ˆœํ•˜๊ฒŒ ๋น„๊ตํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.
  • L์ด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด์ผ๋•Œ ํƒ์ƒ‰, ์‚ฝ์ž…์€ O(L)๋งŒํผ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.
  • ๊ฐ ์ •์ ์ด ์ž์‹์— ๋Œ€ํ•œ ๋งํฌ๋ฅผ ์ „๋ถ€ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ €์žฅ ๊ณต๊ฐ„์„ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.


[3-2] ํŠธ๋ผ์ด ๊ตฌ์กฐ

  • ๋ฃจํŠธ๋Š” ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ ๊ฐ„์„ (๋งํฌ)๋Š” ์ถ”๊ฐ€๋  ๋ฌธ์ž๋ฅผ ํ‚ค๋กœ ๊ฐ€์ง‘๋‹ˆ๋‹ค
  • ๊ฐ ์ •์ ์€ ์ด์ „ ์ •์ ์˜ ๊ฐ’ + ๊ฐ„์„ ์˜ ํ‚ค๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  • ํ•ด์‹œ ํ…Œ์ด๋ธ”๊ณผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


[3-3] ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

class Node {
  constructor(val = "") {
    this.val = val;
    this.children = new Map();
  }
}

class Trie {
  constructor() {
    this.root = new Node();
  }

  insert(str) {
    let curNode = this.root;

    for (const char of str) {
      if (!curNode.children.has(char)) {
        curNode.children.set(char, new Node(curNode.val + char));
      }

      curNode = curNode.children.get(char);
    }
  }

  has(str) {
    let curNode = this.root;

    for (const char of str) {
      if (!curNode.children.has(char)) {
        return false;
      }
      curNode = curNode.children.get(char);
    }

    return true;
  }
}

const trie = new Trie();
trie.insert("sky");
trie.insert("ski");
console.log(trie.has("sky")); // true
console.log(trie.has("ski")); // true
console.log(trie.has("skk")); // false




[4] ์ •๋ ฌ(Sort)

(1) ๋น„๊ต์‹ ์ •๋ ฌ

๋‹ค๋ฅธ ์š”์†Œ์™€ ๋น„๊ต๋ฅผ ํ†ตํ•ด ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ๋ฒ„๋ธ” ์ •๋ ฌ : ์„œ๋กœ ์ธ์ ‘ํ•œ ๋‘ ์š”์†Œ ๊ฒ€์‚ฌํ•˜์—ฌ ์ •๋ ฌ (O(n^2))
  • ์„ ํƒ ์ •๋ ฌ : ์„ ํƒํ•œ ์š”์†Œ์™€ ๊ฐ€์žฅ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์š”์†Œ๋ฅผ ๊ตํ™˜ํ•˜์—ฌ ์ •๋ ฌ - O(n^2)
  • ์‚ฝ์ž… ์ •๋ ฌ : ์„ ํƒํ•œ ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋ฅผ ์ฐพ์•„ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ์‹์˜ ์ •๋ ฌ - O(n^2)


(2) ๋ถ„์‚ฐ์‹ ์ •๋ ฌ

์š”์†Œ๋ฅผ ๋ถ„์‚ฐํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ํ•ฉ๋ณ‘ ์ •๋ ฌ : ๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•œ ์ตœ์„ ๊ณผ ์ตœ์•…์ด ๊ฐ™์€ ์•ˆ์ •์ ์ธ ์ •๋ ฌ ๋ฐฉ๋ฒ• - O(nlog n)
  • ํ€ต ์ •๋ ฌ : ๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•œ ๋งค์šธ ๋น ๋ฅด์ง€๋งŒ ์ตœ์•…์˜ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๋ถˆ์•ˆ์ • ์ •๋ ฌ -O(nlog n)


๋ถ„ํ•  ์ •๋ณต
๋ฌธ์ œ๋ฅผ ์ž‘์€ 2๊ฐœ์˜ ๋ฌธ์ œ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ , ๋” ์ด์ƒ ๋ถ„๋ฆฌ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ• ๋•Œ ์ฒ˜๋ฆฌํ•œ ํ›„ ํ•ฉ์น˜๋Š” ์ „๋žต




[+] ์‹ค์Šต - ๊ฐ€์žฅ ํฐ ์ˆ˜

  • ์ดˆ๊ธฐ ๋‹ต์•ˆ
function solution(numbers) {
  let answer = numbers
    .map((n) => n + "")
    .sort((a, b) => b + a - (a + b))
    .join("");

  return answer;
}

Untitled 4

๐Ÿ˜Š โ€ฆ?

Untitled 5


๊ณฐ๊ณฐํžˆ ์ƒ๊ฐํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ๊ณ ์ณ ๋ณด์•˜์Šต๋‹ˆ๋‹ค ๐Ÿ˜Šโ€ฆ
๋‹ค 0์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ์—ฃ์ง€ ์ผ€์ด์Šค๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š์•˜๋”๋ผ๊ณ ์š”..?

Untitled 6


  • ์ˆ˜์ • ๋‹ต์•ˆ
function solution(numbers) {
  let answer = numbers
    .map((n) => n + "")
    .sort((a, b) => b + a - (a + b))
    .join("");

  if (parseInt(answer) === 0) {
    return "0";
  }

  return answer;
}

Untitled 7




[5] ์ด์ง„ ํƒ์ƒ‰(Binary Search)

์ •๋ ฌ ๋˜์–ด์žˆ๋Š” ์š”์†Œ๋“ค์„ ๋ฐ˜์”ฉ ์ œ์™ธํ•˜๋ฉฐ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

O(lon n)๋งŒํผ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.


[5-1] ์ด์ง„ ํƒ์ƒ‰ ํŠน์ง•

  • ๋ฐ˜๋“œ์‹œ ์ •๋ ฌ์ด ๋˜์–ด์žˆ์–ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฐฐ์—ด ํ˜น์€ ์ด์ง„ ํŠธ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • O(log n) ์‹œ๊ฐ„๋ณต์žก๋„์ธ๋งŒํผ ์ƒ๋‹นํžˆ ๋น ๋ฆ…๋‹ˆ๋‹ค.
  • ์ตœ์•…์˜ ๊ฒฝ์šฐ ํ•œ์ชฝ์œผ๋กœ ํŽธํ–ฅ๋œ ํŠธ๋ฆฌ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ, ์ˆœ์ฐจ ํƒ์ƒ‰๊ณผ ๋™์ผํ•œ ์‹œ๊ฐ„๋ณต์žก๋„(O(n))๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
    โ†’ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด AVL ํŠธ๋ฆฌ, ๋ ˆ๋“œ-๋ธ”๋ž™ ํŠธ๋ฆฌ์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.




[+] ๊ณผ์ œ - ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ ์š”์†Œ ์ œ๊ฑฐ

์ง„์งœ ์ด๋ ‡๊ฒŒ ๊ธด ๋ฉ”์†Œ๋“œ๋Š” ๋‚˜๋„ ์ฒ˜์Œ ์งœ๋ณด๋Š”๊ฑธ..? ๐Ÿ˜ฅ
์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด์„œ ์งฐ๋‹ค๊ฐ€ ํƒˆ์ถœํ•˜์ง€ ๋ชปํ•˜๋Š” ์ด์Šˆ๋•Œ๋ฌธ์—
parentNode์™€,, minParent๋ฅผ ๋”ฐ๋กœ ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

> ์ด๋กœ์„œ ๋ฐฑ์ข…์› ์„ ์ƒ๋‹˜๋„ ๊ฐํƒ„ํ•˜์‹ค ์ŠคํŒŒ๊ฒŒํ‹ฐ ์ฝ”๋“œ ์™„์„ฑ!!
(๋ฆฌํŒฉํ† ๋ง ํ•˜๊ณ  ๋‹ค์‹œ ์˜ฌ๋ ค์•ผ๊ฒ ๋‹ค. ๋„ˆ๋ฌด๋‚˜๋„ ๋ถ€๋„๋Ÿฝ๋‹ค.)

delete(val) {
	let curNode = this.root;
	let parentNode = null;

	while (curNode !== null) {
		// ํƒ์ƒ‰ ์ง„ํ–‰
		if (curNode.val < val) {
			parentNode = curNode;
			curNode = curNode.right;
		}
		else if (curNode.val > val) {
			parentNode = curNode;
			curNode = curNode.left;
		}
		else {
			// ๋ฆฌํ”„๋…ธ๋“œ์ผ ๊ฒฝ์šฐ
			if (curNode.left === null && curNode.right === null) {
				console.log('leaf')
				if (parentNode.val < curNode.val) {
					parentNode.right = null;
					return;
				}
				parentNode.left = null;

				return;
			}
			// ์ž์‹์ด ํ•˜๋‚˜์ผ๋•Œ
			else if (curNode.left && !curNode.right) {
				if (parentNode.val < curNode.val) {
					parentNode.right = curNode.left;
					return curNode;
				}
				parentNode.left = curNode.left

				return curNode;
			}
			else if (!curNode.left && curNode.right) {
				if (parentNode.val < curNode.val) {
					parentNode.right = curNode.right;
					return curNode;
				}
				parentNode.left = curNode.right

				return curNode;
			}

			if (curNode.val < parentNode.val) {
				let min = curNode.right;
				let minParent = curNode.right;

				while (min.left) {
					minParent = min;
					min = min.left;
				}

				if (minParent === min) {
					parentNode.left = min;
					min.left = curNode.lfet;
					curNode.right = null;
				} else {
					if (min.right) {
						minParent.left = min.right;
					}
					else {
						minParent.left = null;
					}
				}

				parentNode.left = min;
				min.right = curNode.right;
				min.left = curNode.left;

			} else {
				let min = curNode.right;
				let minParent = curNode.right;

				while (min.left) {
					minParent = min;
					min = min.left;
				}

				if (minParent === min) {
					parentNode.right = min;
					min.left = curNode.lfet;
					curNode.right = null;
				} else {
					if (min.right) {
						minParent.left = min.right;
					}
					else {
						minParent.left = null;
					}
				}

				parentNode.right = min;
				min.right = curNode.right;
				min.left = curNode.left
			}

			return curNode;
		}
	}

	return null;
	}




[+] ์‹ค์Šต - ์ž…๊ตญ์‹ฌ์‚ฌ

์ž…๋ ฅ์˜ ์ตœ๋Œ€ ๋ฒ”์œ„๊ฐ€ 1,000,000,000์ด๋ฏ€๋กœ ์ด์ง„ ํƒ์ƒ‰์ด๊ฒ ๊ตฌ๋‚˜ ์ถ”์ธกํ–ˆ์Šต๋‹ˆ๋‹ค.
์ด์ง„ํƒ์ƒ‰โ€ฆํ•œ๋ฒˆ๋„ ํ’€์–ด๋ณธ ์  ์—†๋Š”๋ฐ ๐Ÿ˜ฅ


  • ๋‹ต์•ˆ
function binarySearch(n, times) {
  let left = 0;
  let right = n * times[times.length - 1]; // n * ๊ฐ€์žฅ ๊ธด ์‹ฌ์‚ฌ๋Œ€
  let answer = right;

  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    let max = 0;

    for (let t of times) {
      max += Math.floor(mid / t);
    }

    if (max < n) {
      // ์‹ฌ์‚ฌํ•œ ์ธ์›์ด ์ ์–ด์š”
      left = mid + 1;
    } else {
      // ์‹ฌ์‚ฌํ•œ ์ธ์›์ด ์ดˆ๊ณผ๋˜์—ˆ์œผ๋‹ˆ right(์ตœ๋Œ€) - 1
      answer = Math.min(mid, answer);
      right = mid - 1;
    }

    mid = Math.floor((left + right) / 2);
  }

  return answer;
}

function solution(n, times) {
  times.sort((a, b) => a - b); // ์ด๋ถ„ํƒ์ƒ‰์€ ์ •๋ ฌ ํ›„์— ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  return binarySearch(n, times);
}

์ด์ง„ํƒ์ƒ‰ ์ฝ”๋“œ๋ฅผ ๊ฑฐ์˜ ๋ณต๋ถ™ํ•˜๋‹ค์‹œํ”ผ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
JS ์ด์ง„ํƒ์ƒ‰ ์ฝ”๋“œ ์™ธ์šด ํ›„, ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๋งŒํผ ์ดํ•ดํ•ด๋†“์ž.


์ถœ์ฒ˜

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

์นดํ…Œ๊ณ ๋ฆฌ: ,

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ