STUDYING/Algorithm

[Programmers] 타겟 넘버

EOZIN 2021. 9. 27. 00:37
728x90

https://programmers.co.kr/learn/courses/30/lessons/43165

import java.util.*;

class Solution {
    
    static int answer;
    
    public int solution(int[] numbers, int target) {

        dfs(numbers, target, 0);
        
        return answer;
    }
    
    public static void dfs(int[] numbers, int target, int idx) {
        if (idx == numbers.length) {
            if (0 == target) answer++;
            return;
        }
        
        dfs(numbers, target - numbers[idx], idx + 1);
        dfs(numbers, target + numbers[idx], idx + 1);
    }
    
}