일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 다익스트라
- divmod
- Re
- KAKAO BLIND RECRUITMENT
- 카카오
- dfs
- 정렬
- 추석맞이 코딩챌린지
- DateTime
- 이분탐색
- heapq
- python
- 프로그래머스
- java
- 백준
- 그리디
- BFS
- Set
- 위클리 챌린지
- Zip
- Combinations
- 정규식
- 파이썬
- 자바
- 동적 계획법
- programmers
- backjoon
- 재귀함수
- 수학
- lambda
Archives
- Today
- Total
상상쓰
[프로그래머스] 타겟 넘버 본문
https://programmers.co.kr/learn/courses/30/lessons/43165
코딩테스트 연습 - 타겟 넘버
n개의 음이 아닌 정수가 있습니다. 이 수를 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수 있습니다. -1+1+1+1+1 = 3 +1-1+1+1+
programmers.co.kr
단계별로 배열을 만들어(new int[2^i]) 모든 경우의 수를 검사하였다.
class Solution {
public int solution(int[] numbers, int target) {
int answer = 0;
int[] array = {0};
for (int i=0;i<numbers.length;i++) {
int[] temp = new int[array.length * 2];
for (int j=0;j<array.length;j++) {
temp[j * 2] = array[j] + numbers[i];
temp[j * 2 + 1] = array[j] - numbers[i];
}
array = temp;
}
for (int i=0;i<array.length;i++) {
if (array[i] == target) {
answer++;
}
}
return answer;
}
}
'Coding Test' 카테고리의 다른 글
[프로그래머스] 카펫 (0) | 2021.07.28 |
---|---|
[프로그래머스] 프린터 (0) | 2021.07.27 |
[프로그래머스] 네트워크 (0) | 2021.07.26 |
[백준] 스택 (0) | 2021.07.23 |
[프로그래머스] 튜플 (0) | 2021.07.23 |
Comments