notion-030

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

์˜ค๋Š˜์˜ ์†Œ๊ฐ์€?
ํŒŒ์ด์ฌ์œผ๋กœ ์ฝ”ํ…Œ ์ค€๋น„๋ฅผ ํ–ˆ์—ˆ์–ด์„œ, JS๋กœ ํ’€์ž๋‹ˆ ๋„ˆ๋ฌด ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆฌ๋„ค์š”.
์ง€๊ธˆ์ด๋ผ๋„ JS๋กœ ์—ฐ์Šตํ•ด์•ผ ํ•˜๋‚˜.. ๊ณ ๋ฏผ ์ค‘์ž…๋‹ˆ๋‹ค.
์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•ด..๐Ÿ˜ฏ

๊ทธ๋ฆฌ๋””๋Š” ์ง๊ด€์ ์ธ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์ง€๋งŒ,
์ง๊ด€์ ์ธ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด ์ƒ๊ฐ๋‚˜์ง€ ์•Š์œผ๋ฉด ์ ˆ๋Œ€ ๋ชป ํ’€ ๊ฒƒ ๊ฐ™๋„ค์š”.
๋งŽ์ด ํ’€์–ด๋ณด๋Š” ๊ฒƒ์ด ์‹ค๋ ฅ์ด ํ–ฅ์ƒ๋˜๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๊ฒ ์ฃ ?




[1] BFS & DFS


[1-1] BFS (๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰)

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ฐ™์€ ๊นŠ์ด์— ํ•ด๋‹นํ•˜๋Š” ์ •์ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.


ํŠน์ง•

  • Queue๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์‹œ์ž‘ ์ง€์ ์—์„œ ๊ฐ€๊นŒ์šด ์ •์ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
  • V๊ฐ€ ์ •์ ์˜ ์ˆ˜, E๊ฐ€ ๊ฐ„์„ ์˜ ์ˆ˜ ์ผ๋•Œ, BFS์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(V+E)์ž…๋‹ˆ๋‹ค.


[1-2] DFS (๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰)

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ตœ๋Œ€ํ•œ ๊นŠ์€ ์ •์ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.


ํŠน์ง•

  • Stack์„ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์‹œ์ž‘ ์ •์ ์—์„œ ๊นŠ์€ ๊ฒƒ ๋ถ€ํ„ฐ ์ฐพ์Šต๋‹ˆ๋‹ค.
  • V๊ฐ€ ์ •์ ์˜ ์ˆ˜, E๊ฐ€ ๊ฐ„์„ ์˜ ์ˆ˜ ์ผ๋•Œ, DFS์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(V+E)์ž…๋‹ˆ๋‹ค.




[+] ์‹ค์Šต ์ฝ”๋“œ - ์—ฌํ–‰ ๊ฒฝ๋กœ

DFS๋กœ ํ’€์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.


  • ์ œ์ถœ ์ฝ”๋“œ
function solution(tickets) {
  let answer = ["ICN"];
  let visit = new Array(tickets.length).fill(0);

  tickets.sort();

  function dfs(str, index) {
    if (index === tickets.length) {
      return true;
    }

    for (let i = 0; i < tickets.length; i += 1) {
      if (visit[i] == 0 && tickets[i][0] == str) {
        visit[i] = 1;
        answer.push(tickets[i][1]);
        if (dfs(tickets[i][1], index + 1)) {
          return true;
        }
        visit[i] = 0;
        answer.pop();
      }
    }

    return false;
  }

  dfs("ICN", 0);

  return answer;
}




[2] ๊ทธ๋ฆฌ๋””(Greedy)

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

๋”ฐ๋ผ์„œ ์ตœ์  ํ•ด๋ฅผ ๋ณด์žฅํ•ด์ฃผ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.


[2-1] ๊ทธ๋ฆฌ๋”” ํŠน์ง•

  • ์ตœ์  ํ•ด๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ณด๋‹ค ๋น ๋ฅธ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค.
    ๋ณดํ†ต ์„ ํ˜• ์‹œ๊ฐ„์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. (O(n))
  • ํฌ๋ฃจ์Šค์นผ, ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋“ฑ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์ง๊ด€์ ์ธ ๋ฌธ์ œ ํ’€์ด์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.




[+] ์‹ค์Šต ์ฝ”๋“œ - ํฐ ์ˆ˜ ๋งŒ๋“ค๊ธฐ

๐Ÿ˜€ ์ „ํ˜•์ ์ธ ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ์ค€๋น„ํ•˜๋ฉด์„œ ๊ทธ๋ฆฌ๋””๋ฅผ ๋งŽ์ด ์•ˆํ’€์–ด๋ดค์—ˆ๋Š”๋ฐ, ์ด์ œ๋ผ๋„ ์—ด์‹ฌํžˆ ํ’€์–ด๋ด์•ผ๊ฒ ์–ด์š”.

๋งˆ์ง€๋ง‰์— k๊ฐ€ ๋‚จ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•ด์„œ
ํ•˜โ€ฆโ€ฆโ€ฆsplice ๊ตฌ์—ญ ์ƒ๊ฐํ•˜๋Š๋ผ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.


  • ์ œ์ถœ ์ฝ”๋“œ
function solution(number, k) {
  numArr = [];

  for (let i = 0; i < number.length; i += 1) {
    const num = number[i];

    while (k > 0) {
      if (numArr[numArr.length - 1] < num) {
        numArr.pop();
        k -= 1;
      } else break;
    }

    numArr.push(num);
  }

  numArr.splice(numArr.length - k, k);
  answer = numArr.join("");

  return answer;
}

์ถœ์ฒ˜

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

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

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

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