Algorithm

프로그래머스_타켓 넘버

Young_J 2021. 3. 19. 23:56

// 알고리즘

 

1. dfs

 

2. '+' 를 하는경우와 '-' 를 하는경우 둘다 깊이 우선 탐색

 

3. target값고 결과 값이 같으면 ans++

더보기
import java.util.*;

class Solution {
    static int N,tar,ans;
    public int solution(int[] numbers, int target) {
        int answer = 0;
        
        N = numbers.length;
        tar = target;
        
        
        cal(0,0,numbers);
        
        return ans;
    }
    
    public void cal(int idx,int cnt,int[] numbers){
        if(idx == N){
            if(cnt == tar) ans+=1; 
            return;
        }
        
        cal(idx+1,cnt + numbers[idx],numbers);
        cal(idx+1,cnt - numbers[idx],numbers);
            
    }
}

 

 

※ 프로그래머스가 아직 익숙하지 않아서 쉬운문제부터 차근차근 풀어봐야겠다.