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

๋ฌธ์ œ

n๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฆฌ์ŠคํŠธ nums์™€ ์ •์ˆ˜ target์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, num์— ์žˆ๋Š” ์ •์ˆ˜ 4๊ฐœ๋ฅผ ํ•ฉํ•˜์—ฌ target์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐํ•ฉ์„ ๊ตฌํ•˜์‹œ์˜ค.๋‹จ, ์ •๋‹ต์—๋Š” ๋™์ผํ•œ ์ •์ˆ˜ ์กฐํ•ฉ์ด ์—ฌ๋Ÿฌ๊ฐœ ํฌํ•จ๋˜์–ด์„œ๋Š” ์•ˆ๋œ๋‹ค.

 

๋ฌธ์ œํ’€์ด

  1. ์ค‘๋ณต์„ ํ—ˆ๋ฝํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์ฒดํฌ ๋ฐฐ์—ด์„ ์ƒ์„ฑ
  2. ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ •์ˆ˜ ์กฐํ•ฉ์€ ์—ฌ๋Ÿฌ๊ฐœ ํฌํ•จ๋˜๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ„๋„์˜ ๋ณ€์ˆ˜ ์„ ์–ธ
  3. ๊นŠ์ด ํƒ์ƒ‰์„ ํ†ตํ•ด ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์กฐํ•ฉ์„ ์ •๋‹ต ๋ฆฌ์ŠคํŠธ์— ํฌํ•จ์‹œํ‚ค๊ณ , ์ฒ˜์Œ ์žฌ๊ท€๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ ํ•ด๋‹น ์ •๋‹ต ๋ฆฌ์ŠคํŠธ์— ํ˜„์žฌ ๊ฒ€์‚ฌํ•˜๋Š” ์กฐํ•ฉ์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ_์กฐํ•ฉ์€ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๊ณ , ์žˆ๋‹ค๋ฉด ๊ทธ๋Œ€๋กœ ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒ

์ฒ˜์Œ์—๋Š” 3๋ฒˆ ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ ์œ„ํ•ด forEach ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœํšŒํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ์•˜์„ ๋•Œ forEach๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒ์‹œํ‚ค์ง€ ์•Š๊ณ  ์š”์†Œ ๋๊นŒ์ง€ ์ˆœํšŒํ•˜๋ฉด์„œ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ’์„ ์–ป์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

 

forEach ๋ฉ”์„œ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ break๋ฌธ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด์™€ ๊ฐ™์€ ์ผ์ด ์ƒ๊ธด๊ฑด๋ฐ ์ฐพ์•„๋ณด๋‹ˆ Array.prototype.some ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

some() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด ์•ˆ์˜ ์–ด๋–ค ์š”์†Œ๋ผ๋„ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค.

 

์ •๋‹ต ๋ฆฌ์ŠคํŠธ๋Š” 2์ฐจ์› ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— some ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์š”์†Œ์˜ 1์ฐจ์› ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋„๋ก ํ•˜๊ณ , ํ•ด๋‹น 1์ฐจ์› ๋ฐฐ์—ด์„ filiter ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ํ˜„์žฌ ๊ฒ€์‚ฌํ•˜๊ณ ์ž ํ•˜๋Š” ์กฐํ•ฉ ๋ฐฐ์—ด tmp์— ๋Œ€ํ•ด includes ๋ฉ”์„œ๋“œ๋กœ ํ•ด๋‹น ์š”์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ์‹๋ณ„ํ•˜์˜€๋‹ค.

 

๋งŒ์•ฝ์— ํ˜„์žฌ ๊ฒ€์‚ฌํ•˜๊ณ ์ž ํ•˜๋Š” ์กฐํ•ฉ ๋ฐฐ์—ด tmp์˜ ๋ชจ๋“  ๊ฐ’์ด ์ •๋‹ต ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ์กฐํ•ฉ ์ค‘์— ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด includes ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด์„œ ๋ชจ๋“  ๋ฆฌํ„ด ๊ฐ’์ด true๊ฐ€ ๋˜๋ฏ€๋กœ ์ตœ์ข…์ ์œผ๋กœ 1์ฐจ์› ๋ฐฐ์—ด์—์„œ์˜ filiter ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” 4(์ •์ˆ˜ ์กฐํ•ฉ)๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋ž˜์„œ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 4๋ผ๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ๋‹ค๋ฉด ์ดํ›„ ์ˆœํšŒ๋ฅผ ๋” ํ•  ํ•„์š” ์—†์ด some ๋ฉ”์„œ๋“œ๋Š” true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ 3๋ฒˆ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œ์ผฐ๋‹ค.

 

์ฝ”๋“œ

function solution(nums, target) {
  let answer = [];
  let tmp = Array.from({ length: 4 });
  let ch = Array.from({ length: nums.length }, () => 0); 
  let ch2 = false;

  function DFS(L) {
    ch2 = answer.some((arr) => arr.filter((x) => tmp.includes(x)).length === 4);
    if (ch2) return;
    if (L === 4) {
      if (tmp.reduce((a, b) => a + b) === target) {
        answer.push([...tmp]);
      }
    } else {
      for (let i = 0; i < nums.length; i++) {
        if (ch[i] === 0) {
          ch[i] = 1;
          tmp[L] = nums[i];
          DFS(L + 1);
          ch[i] = 0;
        }
      }
    }
  }

  DFS(0);
  return answer;
}

let nums = [1, 0, -1, 0, -2, 2];
console.log(solution(nums, 0));

 

ํŒŒ์ด์ฌ itertools combinations

JS๋ฅผ ํ†ตํ•ด ์กฐ๊ฑด์— ๋งž๋Š” ์กฐํ•ฉ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์†Œ ๊ธด(?) ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.์ด์—  ๋ฐ˜ํ•ด ํŒŒ์ด์ฌ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์กฐํ•ฉ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ„๋„์˜ ํ•จ์ˆ˜์ธ itertools์˜ combinations๊ฐ€ ์žˆ์—ˆ๋‹ค.

def solution(nums, target):
	result = [items for items in intertools.combinations(nums, 4) if sum(items) == target]
    return result
    
print(solution[1, 0, -1, 0, -2, 2], 0)

JS์—์„œ๋„ ์กฐํ•ฉ์„ ๊ตฌํ•˜๋Š” ๋” ๊ฐ„๋‹จํ•˜๊ณ  ํšจ์œจ์ ์ธ ๋ฐฉ์‹์ด ์žˆ๋Š”์ง€ ์ฐพ์•„๋ด์•ผ๊ฒ ๋‹ค.

(ํ˜„์žฌ Array.some ์œผ๋กœ ์กฐํ•ฉ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ ์ƒ ๋ณต์žกํ•˜๊ณ  ์ •์„์ ์ธ ๋ฐฉ๋ฒ•์ด ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋‹ค..)

 

(์ถ”๊ฐ€_2021.08.08)

์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ

์ด์ „์— ์กฐํ•ฉ์— ๋Œ€ํ•œ ์ œ๋Œ€๋กœ ๋œ ์ดํ•ด ์—†์ด Array.some ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐํ•ฉ์˜ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค ๋ผ๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œ์ผฐ๋Š”๋ฐ ์ด๋ณด๋‹ค ๋” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋‹ค.

๋‹ค์Œ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์‚ดํŽด๋ณด์ž.

์กฐํ•ฉ

 

function solution(n, m) {
  let answer = [];
  let tmp = Array.from({length: m}, () => 0);

  function DFS(L, s) {
    if(L === m) {
      answer.push(tmp.slice())
    }
    else {
      for(let i = s; i <= n; i++) { //๋‹ค์Œ ์žฌ๊ท€์—์„œ์˜ ๋ฐ˜๋ณต๋ฌธ ์‹œ์ž‘์€ 1๊ฐ€์‚ฐ
          tmp[L] = i;
          DFS(L + 1, i + 1);
      }
    }
  }
  
  DFS(0, 1);
  return answer;
}

console.log(solution(4, 2));

function solution(m, nums) {
  let answer = [];
  let tmp = Array.from({length: m}, () => 0);
  let n = nums.length;

  function DFS(L, s) {
    if(L === m) {
      if(tmp.reduce((a, b) => a +b) % 6 === 0) answer.push(tmp.slice());
    }
    else {
      for(let i = s; i < n; i++) {
        tmp[L] = nums[i];
        DFS(L + 1, i + 1);
      }
    }
  }
  
  DFS(0, 0); //๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ˆœํšŒ ๋ฐ ์ฐธ์กฐํ•ด์•ผ ๋˜๊ธฐ ๋•Œ๋ฌธ์— start๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘
  return answer;
}

let nums = [2, 4, 5, 8, 12]
console.log(solution(3, 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
๊ธ€ ๋ณด๊ด€ํ•จ