forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.java
More file actions
87 lines (81 loc) Β· 3.22 KB
/
8.java
File metadata and controls
87 lines (81 loc) Β· 3.22 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.*;
class Permutation {
private int n;
private int r;
private int[] now; // νμ¬ μμ΄
private ArrayList<ArrayList<Integer>> result; // λͺ¨λ μμ΄
public ArrayList<ArrayList<Integer>> getResult() {
return result;
}
public Permutation(int n, int r) {
this.n = n;
this.r = r;
this.now = new int[r];
this.result = new ArrayList<ArrayList<Integer>>();
}
public void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public void permutation(int[] arr, int depth) {
// νμ¬ μμ΄μ κΈΈμ΄κ° rμΌ λ κ²°κ³Ό μ μ₯
if (depth == r) {
ArrayList<Integer> temp = new ArrayList<>();
for (int i = 0; i < now.length; i++) {
temp.add(now[i]);
}
result.add(temp);
return;
}
for (int i = depth; i < n; i++) {
swap(arr, i, depth);
now[depth] = arr[depth];
permutation(arr, depth + 1);
swap(arr, i, depth);
}
}
}
class Solution {
public int solution(int n, int[] weak, int[] dist) {
// κΈΈμ΄λ₯Ό 2λ°°λ‘ λλ €μ 'μν'μ μΌμ ννλ‘ λ³κ²½
ArrayList<Integer> weakList = new ArrayList<Integer>();
for (int i = 0; i < weak.length; i++) {
weakList.add(weak[i]);
}
for (int i = 0; i < weak.length; i++) {
weakList.add(weak[i] + n);
}
// ν¬μ
ν μΉκ΅¬ μμ μ΅μκ°μ μ°ΎμμΌ νλ―λ‘ len(dist) + 1λ‘ μ΄κΈ°ν
int answer = dist.length + 1;
// μΉκ΅¬ μ 보λ₯Ό μ΄μ©ν΄ λͺ¨λ μμ΄ κ³μ°
Permutation perm = new Permutation(dist.length, dist.length);
perm.permutation(dist, 0);
ArrayList<ArrayList<Integer>> distList = perm.getResult();
// 0λΆν° length - 1κΉμ§μ μμΉλ₯Ό κ°κ° μμμ μΌλ‘ μ€μ
for (int start = 0; start < weak.length; start++) {
// μΉκ΅¬λ₯Ό λμ΄νλ λͺ¨λ κ²½μ° κ°κ°μ λνμ¬ νμΈ
for (int i = 0; i < distList.size(); i++) {
int cnt = 1; // ν¬μ
ν μΉκ΅¬μ μ
// ν΄λΉ μΉκ΅¬κ° μ κ²ν μ μλ λ§μ§λ§ μμΉ
int position = weakList.get(start) + distList.get(i).get(cnt - 1);
// μμμ λΆν° λͺ¨λ μ·¨μ½ν μ§μ μ νμΈ
for (int index = start; index < start + weak.length; index++) {
// μ κ²ν μ μλ μμΉλ₯Ό λ²μ΄λλ κ²½μ°
if (position < weakList.get(index)) {
cnt += 1; // μλ‘μ΄ μΉκ΅¬λ₯Ό ν¬μ
if (cnt > dist.length) { // λ ν¬μ
μ΄ λΆκ°λ₯νλ€λ©΄ μ’
λ£
break;
}
position = weakList.get(index) + distList.get(i).get(cnt - 1);
}
}
answer = Math.min(answer, cnt); // μ΅μκ° κ³μ°
}
}
if (answer > dist.length) {
return -1;
}
return answer;
}
}