dfs 문제입니다.
dfs를 통해 모든 경우의 수. 즉, 완전 탐색을 하면서 조건에 맞는 것을 찾으면 됩니다,
[구현 코드]
class Solution {
private static int cnt = 0;
public static int solution(int[] numbers, int target) {
dfs(0, numbers, target, 0);
return cnt;
}
private static void dfs(int number, int[] numbers, int target, int depth) {
if (depth == numbers.length) {
if (number == target) {
cnt++;
}
return;
}
// +일 때
dfs(number+numbers[depth], numbers, target, depth+1);
// -일 때
dfs(number-numbers[depth], numbers, target, depth+1);
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 - 최고의 집합 (0) | 2022.10.21 |
---|---|
프로그래머스 - N으로 표현(Java) (0) | 2022.10.21 |
프로그래머스 - 삼각 달팽이(Java) (0) | 2022.10.08 |
프로그래머스 - 풍선 터트리기(Java) (0) | 2022.10.07 |
프로그래머스 - 숫자 게임(Java) (0) | 2022.10.06 |