티스토리 뷰
// 알고리즘
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 |