일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 자바 버전
- 개발자
- VUE
- Spring AOP
- 도커
- java version
- axios
- 백엔드
- transaction
- Java
- RequestParam
- vue life cycle
- 비동기통신
- fetch
- github
- 트랜잭션
- 프론트엔드
- PathVariable
- RequestBody
- docker
- Vue.js
- aop
- git push
- GIT
- gradle
- 빌드
- maven
Archives
- Today
- Total
미소의 세상
[알고리즘] 타겟넘버 본문
https://programmers.co.kr/learn/courses/30/lessons/43165?language=java
코딩테스트 연습 - 타겟 넘버
n개의 음이 아닌 정수들이 있습니다. 이 정수들을 순서를 바꾸지 않고 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수
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 depth, int sum) {
int matchCount = 0;
if (depth == numbers.length) {
if (sum == target) {
return 1;
}
return 0;
}
matchCount += dfs(numbers, target, depth + 1, sum + numbers[depth]);
matchCount += dfs(numbers, target, depth + 1, sum - numbers[depth]);
return matchCount;
}
}
'알고리즘 > 알고리즘 풀이' 카테고리의 다른 글
[알고리즘] 키패드 누르기 (0) | 2022.04.10 |
---|---|
[알고리즘] 완주하지 못한 선수 (0) | 2022.03.14 |
[알고리즘] 나누어 떨어지는 숫자 배열 (0) | 2022.03.10 |
[알고리즘] 정렬-K번째수 (0) | 2022.02.28 |
[알고리즘] 신규 아이디 추천 (0) | 2022.02.27 |
Comments