호지

[프로그래머스] 타겟 넘버 문제풀이 JS 본문

알고리즘/프로그래머스

[프로그래머스] 타겟 넘버 문제풀이 JS

_hoji

간단한 DFS문제이다.

각 number를 더하거나 뺐을 때, 주어진 target이 되는 경우의 수는 몇개가 있는지 구하는 문제이다.

DFS로 모든 경우의 수를 탐색해서 target의 개수를 구했다.

function solution(numbers, target) {
    let answer = 0;
    DFS(0,0);
    function DFS(l, sum){
      if(l === numbers.length){
          if(sum === target) answer ++;
      }
      else{
          DFS(l+1, sum+numbers[l])
          DFS(l+1, sum-numbers[l])
      }
  }
  
    return answer;
}
Comments