-
[프로그래머스] 타겟 넘버Algorithm 2020. 9. 15. 12:27반응형
n개의 음이 아닌 정수가 있습니다. 이 수를 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수 있습니다.
-1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+1-1+1 = 3 +1+1+1+1-1 = 3
사용할 수 있는 숫자가 담긴 배열 numbers, 타겟 넘버 target이 매개변수로 주어질 때 숫자를 적절히 더하고 빼서 타겟 넘버를 만드는 방법의 수를 return 하도록 solution 함수를 작성해주세요.
static public int solution(int[] numbers, int target) { return dfs(numbers, target, 0, 0); } static public int dfs(int[] numbers, int target, int index, int num) { if (index == numbers.length){ return num == target ? 1 : 0;} else return dfs(numbers, target, index + 1, num + numbers[index]) + dfs(numbers, target, index + 1, num - numbers[index]); }
반응형'Algorithm' 카테고리의 다른 글
[프로그래머스] K번째수 (0) 2020.09.15 [프로그래머스] 크레인인형뽑기게임 (0) 2020.09.15 [프로그래머스] 탑 (0) 2020.09.15 [프로그래머스] 스킬 트리 (0) 2020.09.15 [프로그래머스] 소수 만들기 (0) 2020.09.15