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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| public class programmers_외벽점검_kgh { static boolean[] check; static List<Integer> circleWeakList = new ArrayList<>(); static LinkedList<Integer> friendDistList = new LinkedList<>(); static int answer = Integer.MAX_VALUE; public static void main(String[] args) { solution(12, new int[]{1,5,6,10},new int[]{1,2,3,4}); } static int solution(int n, int[] weak, int[] dist) { check = new boolean[dist.length];
for(int i=0; i<weak.length; i++){ circleWeakList.add(weak[i]); } for(int i=0; i<weak.length; i++){ circleWeakList.add(weak[i]+n); } dfs(0,weak,dist); answer = (answer == Integer.MAX_VALUE) ? -1 : answer; return answer; }
private static void dfs(int cnt, int[] weak, int[] dist) { if(cnt == dist.length){ distCheck(weak); return; } for(int i=0; i<dist.length; i++){ if(check[i]){ continue; } check[i] = true; friendDistList.add(dist[i]); dfs(cnt+1, weak, dist); friendDistList.removeLast(); check[i] = false; } } private static void distCheck(int[] weak) { for(int i=0; i<weak.length; i++){ int idx = 0; boolean friendSizeCheck = false; int startPoint = circleWeakList.get(i); for(int j=i; j<i+weak.length; j++){ if(friendDistList.get(idx) < circleWeakList.get(j) - startPoint){ startPoint = circleWeakList.get(j); idx++; if(idx == friendDistList.size()){ friendSizeCheck = true; break; } } } if(!friendSizeCheck){ answer = Math.min(answer, idx+1); } } } }
|