-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBeautiful_526.java
More file actions
46 lines (43 loc) · 1.2 KB
/
Beautiful_526.java
File metadata and controls
46 lines (43 loc) · 1.2 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
import java.util.ArrayList;
import java.util.List;
public class Beautiful_526 {
// Standard Solution Approach #3
int count = 0;
public int countArrangement(int N) {
boolean[] visited = new boolean[N + 1];
calculate(N, 1, visited);
return count;
}
public void calculate(int N, int pos, boolean[] visited) {
if (pos > N)
count++;
for (int i = 1; i <= N; i++) {
if (!visited[i] && (pos % i == 0 || i % pos == 0)) {
visited[i] = true;
calculate(N, pos + 1, visited);
visited[i] = false;
}
}
}
/*
int count = 0;
public int countArrangement(int N) {
backtrack(new ArrayList<>(), N);
return count;
}
private void backtrack(List<Integer> ls, int N) {
if (ls.size() == N)
count++;
else {
for (int i = 1; i <= N; i++) {
int pos = ls.size() + 1;
if (!ls.contains(i) && (i % pos == 0 || pos % i == 0)) {
ls.add(i);
backtrack(ls, N);
ls.remove(ls.size() - 1);
}
}
}
}
*/
}