https://school.programmers.co.kr/learn/courses/30/lessons/60062
참고 풀이
for(int i = 0; i < weak.length; i++) {
weakList.add(weak[i]);
weakList.add(weak[i] + n);
}
public static void permutation(int depth, int[] arr) {
if(depth == distLength) {
distList.add(arr.clone());
return;
}
for(int i = 0; i < distLength; i++) {
if(!distVisit[i]) {
distVisit[i] = true;
arr[depth] = i;
permutation(depth+1, arr);
distVisit[i] = false;
}
}
}
import java.util.*;
class Solution {
int distLength;
ArrayList<Integer> weakList = new ArrayList<>();
ArrayList<int[]> distList = new ArrayList<>();
boolean[] distVisit;
public int solution(int n, int[] weak, int[] dist) {
distLength = dist.length;
distVisit = new boolean[distLength];
int answer = distLength + 1;
for(int i = 0; i < weak.length; i++){
weakList.add(weak[i]);
weakList.add(weak[i]+n);
}
Collections.sort(weakList);
permutation(0, new int[distLength]);
for(int start = 0; start < weak.length; start++){
for(int[] distArr : distList){
int cnt = 1;
int lastPos = weakList.get(start) + dist[distArr[cnt - 1]];
for(int j = start; j < start + weak.length; j++){
if(lastPos < weakList.get(j)){
cnt += 1;
if(cnt > distLength){
break;
}
lastPos = weakList.get(j) + dist[distArr[cnt - 1]];
}
}
answer = Math.min(answer, cnt);
}
}
if(answer > distLength){
return -1;
} else{
return answer;
}
}
public void permutation(int depth, int[] arr){
if(depth == distLength){
distList.add(arr.clone());
return;
}
for(int i = 0; i < distLength; i++){
if(!distVisit[i]){
distVisit[i] = true;
arr[depth] = i;
permutation(depth+1, arr);
distVisit[i] = false;
}
}
}
}