Skip to content

Commit cbbe3b8

Browse files
committed
526. Beautiful Arrangement
1 parent 28b3ff7 commit cbbe3b8

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

‎526. Beautiful Arrangement.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
function countArrangement(N) {
6+
let count = 0;
7+
const used = [];
8+
dfs(1);
9+
return count;
10+
11+
function dfs(idx) {
12+
if (idx > N) {
13+
count++;
14+
return;
15+
}
16+
17+
for (let i = 1; i <= N; i++) {
18+
if (used[i]) continue;
19+
if (i%idx !== 0 && idx%i !== 0) continue;
20+
used[i] = true;
21+
dfs(idx+1);
22+
used[i] = false;
23+
}
24+
}
25+
}
26+
27+
// var countArrangement = function(n) {
28+
// if(n<=0) return 0
29+
// const perms = permutations(n)
30+
// let count = 0
31+
// for(let perm of perms) {
32+
// let valid = true
33+
// for(let i=0; i<perm.length; i++) {
34+
// if(perm[i]%(i+1)===0 || (i+1)%perm[i]===0)
35+
// continue
36+
// else {
37+
// valid = false
38+
// break
39+
// }
40+
// }
41+
// if(valid) count++
42+
// }
43+
// return count
44+
// };
45+
46+
// function permutations(n, way=[], result=[]) {
47+
// if(way.length===n) {
48+
// result.push([...way])
49+
// return
50+
// }
51+
// for(let i=0; i<n; i++) {
52+
// if(!way.includes(i+1)) {
53+
// way.push(i+1)
54+
// permutations(n, way, result)
55+
// way.pop()
56+
// }
57+
// }
58+
// return result
59+
// }

0 commit comments

Comments
 (0)