ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง€๋ฉด 1๋ถ€ํ„ฐ N๊ฐœ๊นŒ์ง€์˜ ์›์†Œ๋ฅผ ๊ฐ–๋Š” ์ง‘ํ•ฉ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

๋ฌธ์ œํ’€์ด


  • ์›์†Œ๊ฐ€ ํฌํ•จ๋˜๋ƒ(1) ํฌํ•จ๋˜์ง€ ์•Š๋Š๋ƒ(0)์— ๋”ฐ๋ผ ๋‘ ๊ฐ„์„ ์œผ๋กœ ๋‚˜๋ˆ”
  • ์ž์—ฐ์ˆ˜ N๊ฐœ๊นŒ์ง€ ํƒ์ƒ‰์„ ๊ณ„์† ํ•˜๊ณ  N์˜ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„œ๊ฒŒ ๋˜๋ฉด ํƒ์ƒ‰์„ ์ข…๋ฃŒ ๋ฐ ๋ถ€๋ถ„์ง‘ํ•ฉ ์ถœ๋ ฅ
  • ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ง€๊ธˆ๊นŒ์ง€ ํƒ์ƒ‰ ํ–ˆ๋˜ ์›์†Œ๋“ค์ด ํฌํ•จ์ธ์ง€ ๋ฏธํฌํ•จ์ธ์ง€ ์ฒดํฌํ•ด๋‘” ๋ณ„๋„์˜ ๋ณ€์ˆ˜ ํ•„์š”

์ฝ”๋“œ

function solution(n) {
  let answer = [];
  let check = Array.from({length: n + 1}, () => 0); //ํฌํ•จ์ธ์ง€ ๋ฏธํฌํ•จ์ธ์ง€ ์ฒดํฌํ•˜๋Š” ๋ฐฐ์—ด(์ž์—ฐ์ˆ˜ 1๋ถ€ํ„ฐ ์‹œ์ž‘์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฒดํฌ ๋ฐฐ์—ด ๋˜ํ•œ 1๊ฐ€์‚ฐ)
  function DFS(v) {
    if(v === n + 1) { //N์˜ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„ฐ์„ ๊ฒฝ์šฐ
      let tmp = '';
      for(let i = 1; i < check.length; i++) {
        if(check[i] === 1) tmp += i + ''; //ํฌํ•จ(1)์ผ ๊ฒฝ์šฐ์˜ ์›์†Œ๋“ค๋งŒ ์ถœ๋ ฅ
      }
      if(tmp.length > 0) answer.push(tmp.trim());
    }
    else {
      check[v] = 1; //ํฌํ•จ
      DFS(v + 1); //์™ผ์ชฝ ๊ฐ„์„ ์œผ๋กœ ํƒ์ƒ‰
      check[v] = 0; //๋ฏธํฌํ•จ
      DFS(v + 1); //์˜ค๋ฅธ์ชฝ ๊ฐ„์„ ์œผ๋กœ ํƒ์ƒ‰
    }
  }
  
  DFS(1);
  return answer;
}

console.log(solution(3));
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ