Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- jenkins
- to display the conditions report re-run your application with 'debug' enabled
- 호석이두마리치킨
- 백준
- Java
- 별자리 만들기
- docker
- 이클립스
- 투에모스문자열
- 프로그래머스
- Error
- documentationpluginsbootstrapper
- 날짜일수
- 20055
- 2167. 2차원 배열의 합
- 자바
- 소가길을건너간이유6
- EC2
- dockercompose
- 2108_통계학
- CMD
- 알고리즘
- 21278
- 18222
- Eclipse
- 14466
- 이산수학
- 설정
- Error fetching remote repo 'origin'
- SpringBoot
Archives
- Today
- Total
계단을 오르듯이
[JAVA] 타겟 넘버 본문
'+' 와 '-' 를 모든 경우에 대입하여 알아보는 문제이므로, 부분집합을 이용해서 선택되면 +를 안되면 -를 연산하도록 했다.
class Solution {
static int count;
public int solution(int[] numbers, int target) {
int answer = 0;
boolean[] add = new boolean[numbers.length];
subset(0,add,numbers,target);
answer = count;
return answer;
}
private static void subset(int cnt, boolean[] add, int[] numbers, int target){
if(cnt == numbers.length){
int sum = 0;
for(int i=0;i<add.length;i++){
if(add[i]) sum += numbers[i];
else sum -= numbers[i];
}
if(sum == target) count++;
return;
}
add[cnt] = true;
subset(cnt+1, add, numbers, target);
add[cnt] = false;
subset(cnt+1, add, numbers, target);
}
}
다른 분의 우수한 dfs코드이다!! 원래 이 문제는 dfs/bfs로 분류되어 있던 문제이다.
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[JAVA] 프로그래머스_베스트 앨범 (0) | 2022.01.12 |
---|---|
[JAVA] 네트워크 (0) | 2022.01.08 |
[JAVA] 소수 찾기(완전탐색) (0) | 2022.01.07 |
[JAVA] 가장 큰 수 (0) | 2022.01.07 |
[JAVA] 기능 개발 (0) | 2022.01.07 |