https://school.programmers.co.kr/learn/courses/30/lessons/43165
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
깊이 우선 탐색을 사용하여 푸는 문제이다.
배열의 값에 직접 * -1을 할까 하였지만 sum에 -해주거나 +기를 해주며 풀수 있었다.
dfs를 오랜만에 어렵긴했지만 얼른 많이 풀어 실력을 쌓아야겠다.
class Solution {
int answer = 0;
//[4, 1, 2, 1] 4 2
public void dfs(int depth, int sum, int[] numbers, int target ){
if(depth == numbers.length){
{
if(target == sum ){
answer++;
return;
}
return;
}
}else{
dfs(depth +1, sum + numbers[depth], numbers, target);
dfs(depth +1, sum - numbers[depth], numbers, target);
}
}
public int solution(int[] numbers, int target) {
dfs(0, 0, numbers, target);
return answer;
}
}
반응형
'프로그래머스' 카테고리의 다른 글
프로그래머스99클럽 코테 스터디 13일차 TIL 이진 변환 반복하기(String문자열의 비교) (0) | 2024.04.10 |
---|---|
프로그래머스 네트워크 - Java (0) | 2024.04.09 |
99클럽 코테 스터디 12일차 TIL 덧칠하기 - 자바(Java) (0) | 2024.04.09 |
프로그래머스 최소직사각형 - 자바(Java) (0) | 2024.04.09 |
99클럽 코테 스터디 11일차 TIL 괄호 회전하기 (2) | 2024.04.08 |