문제 설명
https://school.programmers.co.kr/learn/courses/30/lessons/43165

제한 사항

입출력 예

풀이
class Solution {
int answer = 0;
int[] numbers;
int target;
public int solution(int[] numbers, int target) {
this.numbers = numbers;
this.target = target;
doDfs(0, 0);
return answer;
}
public void doDfs(int index ,int result) {
int result1 = result + numbers[index];
int result2 = result - numbers[index];
if (index == numbers.length - 1) {
if (result1 == target) ++answer;
if (result2 == target) ++answer;
}
if (index < numbers.length - 1) {
doDfs(index + 1, result1);
doDfs(index + 1, result2);
}
}
}
후기
BFS와 DFS 둘 중 하나로 골라서 풀면 된다. 그렇게 어려울 건 없었던 문제
'코딩테스트 (프로그래머스) > Java' 카테고리의 다른 글
| [프로그래머스][JAVA][Lv. 2] k진수에서 소수 개수 구하기 (0) | 2023.09.20 |
|---|---|
| [프로그래머스][Java][Lv. 2] 전화번호 목록 (0) | 2023.09.19 |
| [프로그래머스][JAVA][Lv. 0] OX퀴즈 (0) | 2023.09.18 |
| [프로그래머스][JAVA][Lv. 2] 피로도 (0) | 2023.09.17 |
| [프로그래머스][JAVA][Lv. 4] 도둑질 (0) | 2023.09.16 |