-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
28 lines (27 loc) · 870 Bytes
/
Solution.java
File metadata and controls
28 lines (27 loc) · 870 Bytes
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
class Solution {
public int countArrangement(int N) {
int[] res = new int[]{0};
List<Integer> candidates = new ArrayList<Integer>();
for (int i = 1; i <= N; i++) {
candidates.add(i);
}
backTrack(candidates, 0, res);
return res[0];
}
public void backTrack(List<Integer> candidates, int i, int[] res) {
int size = candidates.size();
if (size == 0) {
res[0]++;
} else {
i++;
for (int j = 0; j < size; j++) {
int c = candidates.get(j);
if (c % i == 0 || i % c == 0) {
List<Integer> tempCandidates = new ArrayList<Integer>(candidates);
tempCandidates.remove(j);
backTrack(tempCandidates, i, res);
}
}
}
}
}