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

๋ฌธ์ œ

N๊ฐœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑ๋œ ์ž์—ฐ์ˆ˜ ์ง‘ํ•ฉ์ด ์ฃผ์–ด์ง€๋ฉด, ์ด ์ง‘ํ•ฉ์„ ๋‘ ๊ฐœ์˜ ๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋‘ ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ์›์†Œ์˜ ํ•ฉ์ด ์„œ๋กœ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๋ฉด “YES"๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ”NO"๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
๋‘˜๋กœ ๋‚˜๋‰˜๋Š” ๋‘ ๋ถ€๋ถ„์ง‘ํ•ฉ์€ ์„œ๋กœ์†Œ ์ง‘ํ•ฉ์ด๋ฉฐ, ๋‘ ๋ถ€๋ถ„์ง‘ํ•ฉ์„ ํ•ฉํ•˜๋ฉด ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์›๋ž˜์˜ ์ง‘ํ•ฉ์ด ๋˜์–ด ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด {1, 3, 5, 6, 7, 10}์ด ์ž…๋ ฅ๋˜๋ฉด {1, 3, 5, 7} = {6, 10} ์œผ๋กœ ๋‘ ๋ถ€๋ถ„์ง‘ํ•ฉ์˜ ํ•ฉ์ด 16์œผ๋กœ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

๋ฌธ์ œํ’€์ด_๋ฌถ์Œ ๊ธฐ์ค€


  • ๋ฌถ์„ ๊ฒฝ์šฐ์™€ ๋ฌถ์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋‘ ๊ฐœ๋กœ ๋‚˜๋ˆ  ๋‹ค์Œ ๋ ˆ๋ฒจ๋กœ ์ด๋™
  • depth๋ฅผ ๋„˜์–ด์„ค ๊ฒฝ์šฐ ํƒ์ƒ‰ ์ข…๋ฃŒ ๋ฐ ๋‚˜๋‰œ ๋‘ ์ง‘ํ•ฉ์„ ๋น„๊ต

์ฝ”๋“œ

function solution(d, n) {
  let answer = 'NO';
  let flag = 0;
  let check = Array.from({length: d + 1}, () => 0);

  function DFS(v) {
    if(v === d + 1) {
      if(flag) return;
      let tmp1 = 0; //1๋ฒˆ ์ง‘ํ•ฉ์˜ ํ•ฉ
      let tmp2 = 0; //2๋ฒˆ ์ง‘ํ•ฉ์˜ ํ•ฉ
      for(let i = 1; i < check.length; i++) {
        if(check[i] === 1) tmp1 += n[i-1];
        else tmp2 += n[i-1];
      }
      if(tmp1 === tmp2) {
        answer ='YES';
        flag = 1;
      }
    }
    else {
      check[v] = 1; //1๋ฒˆ ์ง‘ํ•ฉ์— ๋ฌถ์„ ๊ฒฝ์šฐ
      DFS(v + 1);
      check[v] = 2; //2๋ฒˆ ์ง‘ํ•ฉ์— ๋ฌถ์„ ๊ฒฝ์šฐ
      DFS(v + 1);
    }
  }
  DFS(1);
  return answer;
}

let depth = 6;
let nums = [1, 3, 5, 6, 7, 10];

console.log(solution(depth, nums));

 

๋ฌธ์ œํ’€์ด_ํ•ฉ ๊ธฐ์ค€


  • ํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ์™€ ํ•ฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆ  ์ง„ํ–‰
  • ํ•ฉํ•˜๋“  ํ•ฉํ•˜์ง€ ์•Š๋“  ๊ฐ„์— ๋ ˆ๋ฒจ์€ ๋ฌด์กฐ๊ฑด ์ฆ๊ฐ€
  • ํƒ์ƒ‰์ด ์ข…๋ฃŒ๋˜๋ฉด ํ•ฉํ•œ ๊ฐ’๊ณผ ํ•ฉํ•˜์ง€ ์•Š์€ ๋‚˜๋จธ์ง€ ๊ฐ’(์—ฌ์ง‘ํ•ฉ์˜ ๊ฐ’)์„ ๋น„๊ต

์ฝ”๋“œ

function solution(arr) {
  let answer = 'NO';
  let flag = 0;
  let total = arr.reduce((a, b) => a + b, 0); //๋ชจ๋“  ์›์†Œ์˜ ํ•ฉ
  let n = arr.length;

  function DFS(level, sum) {
    if(flag) return;
    if(level === n) {
      if(sum === (total - sum)) { //์—ฌ์ง‘ํ•ฉ ์›์†Œ ํ•ฉ๊ณผ์˜ ๋น„๊ต
        answer = 'YES';
        flag = 1 ;
      }
    }
    else {
      DFS(level + 1, sum + arr[level]); //ํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ
      DFS(level + 1, sum); //ํ•ฉํ•˜์ง€ ์•Š๊ณ  ๋‹ค์Œ ๋ ˆ๋ฒจ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ
    }
  }
  DFS(0, 0); //๋ ˆ๋ฒจ๊ณผ ์ถ•์  ํ•ฉ
  return answer;
}

let nums = [1, 3, 5, 6, 7, 10];
console.log(solution(nums));
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
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
๊ธ€ ๋ณด๊ด€ํ•จ