▸ 타겟 넘버
▸ 최솟값 만들기
출처 : 코딩테스트 연습 깊이/너비 우선 탐색(DFS/BFS) 타겟 넘버
class Solution {
public int count = 0;
public int solution(int[] numbers, int target) {
int answer = 0;
dfs(numbers, 0, target, 0);
answer = count;
return answer;
}
public void dfs(int[] numbers, int depth, int target, int result) {
if(depth == numbers.length) {
if(target == result) {
count++;
}
return;
}
int plus = result + numbers[depth];
int minus = result - numbers[depth];
dfs(numbers, depth + 1, target, plus);
dfs(numbers, depth + 1, target, minus);
}
}
출처 : 프로그래머스 코딩테스트 연습 > 연습문제 > 최솟값 만들기
import java.util.*;
class Solution
{
public int solution(int[] A, int[] B)
{
int answer = 0;
//오름차순 정렬
Arrays.sort(A);
//내림차순 정렬
Integer[] BInteger = Arrays.stream(B).boxed().toArray(Integer[]::new);
Arrays.sort(BInteger, Collections.reverseOrder());
for(int i = 0; i < A.length; i++) {
answer += A[i]*BInteger[i];
}
return answer;
}
}
import java.util.*;
class Solution
{
public int solution(int[] A, int[] B) {
int answer = 0;
PriorityQueue<Integer> qA = new PriorityQueue<>();
PriorityQueue<Integer> qB = new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < A.length; i++) {
qA.add(A[i]);
qB.add(B[i]);
}
while (!qA.isEmpty()) {
answer += qA.poll() * qB.poll();
}
return answer;
}
}
PriorityQueue<Integer> queue = new PriorityQueue<>();
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());