티스토리 뷰

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);
            
    }
}

 

 

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

'Algorithm' 카테고리의 다른 글

알고리즘 감잡기  (1) 2023.11.28
백준_앱_7579  (0) 2021.03.23
백준_행성 터널_2887  (0) 2021.03.16
백준_비밀 모임_13424  (0) 2021.03.12
백준_거의최단경로_5719  (0) 2021.03.11
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2025/02   »
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
글 보관함