https://school.programmers.co.kr/learn/courses/30/lessons/43165
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
dfs로 완전 탐색해서 풀어주었다.
class Solution {
public static int tmpSum = 0;
public static int answer = 0;
public int solution(int[] numbers, int target) { // 더하거나 빼거나
dfs(numbers, target, 0, 0);
return answer;
}
public void dfs(int[] numbers, int target, int sum, int here) { // 빼거나 더하거나
if(here == numbers.length) {
if(sum == target) {
answer++;
return;
}
return;
}
dfs(numbers, target, sum + numbers[here], here + 1);
dfs(numbers, target, sum - numbers[here], here + 1);
}
}
'Backend > 알고리즘' 카테고리의 다른 글
[프로그래머스] Lv.2 모음 사전 (0) | 2024.05.28 |
---|---|
[프로그래머스] Lv.2 게임 맵 최단거리 (0) | 2024.05.28 |
[프로그래머스] Lv2. 전화번호 목록 (0) | 2024.05.18 |
[백준] 1068번 - 트리 (0) | 2024.05.17 |
[프로그래머스] Lv2. 피로도 (0) | 2024.05.17 |