https://school.programmers.co.kr/learn/challenges
코딩테스트 연습 | 프로그래머스 스쿨
개발자 취업의 필수 관문 코딩테스트를 철저하게 연습하고 대비할 수 있는 문제를 총망라! 프로그래머스에서 선발한 문제로 유형을 파악하고 실력을 업그레이드해 보세요!
school.programmers.co.kr
class Solution {
public int solution(int[] numbers, int target) {
return dfs(numbers, target, 0,0);
}
private int dfs(int[] numbers, int target, int index, int sum) {
// 모든 숫자를 사용한 경우
if (index == numbers.length) {
// 합이 타겟과 같으면 1 반환
return sum == target ? 1 : 0;
}
// 현재 숫자를 더하거나 빼는 두 가지 경우를 재귀적으로 탐색
int add = dfs(numbers, target, index + 1, sum + numbers[index]);
int subtract = dfs(numbers, target, index + 1, sum - numbers[index]);
// 두 경우의 수를 합산
return add + subtract;
}
}
728x90
'Back-End > TIL' 카테고리의 다른 글
프로그래머스 - 체육복 (1) | 2025.04.28 |
---|---|
백준 17298 -TIL (0) | 2025.04.20 |
백준 2023 TIL (0) | 2025.04.20 |
회원탈퇴 구현하기 (soft-delete) -TIL (0) | 2025.02.18 |