https://school.programmers.co.kr/learn/courses/30/lessons/42895
import java.util.*;
class Solution {
public int solution(int N, int number) {
if(N == number){
return 1;
}
List<Set<Integer>> list = new ArrayList<>();
for(int i = 0; i <= 8; i++){
list.add(new HashSet());
}
list.get(1).add(N);
for(int i = 2; i <= 8; i++){
Set<Integer> countSet = list.get(i);
for(int j = 1; j <= i; j++){
Set<Integer> preSet = list.get(j);
Set<Integer> postSet = list.get(i - j);
for(Integer pre : preSet){
for(Integer post : postSet){
countSet.add(pre + post);
countSet.add(pre - post);
countSet.add(pre * post);
if(post != 0){
countSet.add(pre / post);
}
}
}
}
countSet.add(Integer.parseInt(String.valueOf(N).repeat(i)));
if(countSet.contains(number)){
return i;
}
}
return -1;
}
}