원본 문제 : https://programmers.co.kr/learn/courses/30/lessons/43165
문제 풀이 : https://lkhlkh23.tistory.com/74
문제 설명
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 함수를 작성해주세요.
제한사항
- 주어지는 숫자의 개수는 2개 이상 20개 이하입니다.
- 각 숫자는 1 이상 50 이하인 자연수입니다.
- 타겟 넘버는 1 이상 1000 이하인 자연수입니다.
입출력 예
numbers ; target ; return
[1, 1, 1, 1, 1] | 3 | 5 |
입출력 예 설명
문제에 나온 예와 같습니다.
public class Programmers43165 {
public static void main(String[] args) {
int[] numbers = {1,1,1,1,1};
int target = 3;
System.out.println(solution(numbers,target));
}
public static int solution(int[] numbers, int target) {
return dfs(numbers, target, 0, 0);
}
public static 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]);
}
}
}
'프로그래머스 > 코딩연습1' 카테고리의 다른 글
[프로그래머스] 다음 큰 숫자(java)(12911) (0) | 2019.06.05 |
---|---|
[프로그래머스] 가장 큰 정사각형 찾기 (java)(12905) (0) | 2019.06.05 |
[프로그래머스] H-Index (42747)(java) (0) | 2019.06.04 |
[프로그래머스] (못품)조이스틱 (42860)(java) (0) | 2019.06.04 |
[프로그래머스] 더 맵게 (42626)(java) (0) | 2019.06.03 |
댓글