Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 54f6cb1

Browse files
committed
Beautiful Arrangement
1 parent 77af6e4 commit 54f6cb1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Source : https://leetcode.com/problems/beautiful-arrangement/?tab=Description
2+
// Author : Han Zichi
3+
// Date : 2016年02月27日
4+
5+
/**
6+
* @param {number} N
7+
* @return {number}
8+
*/
9+
var countArrangement = function(N) {
10+
let [ans, hash] = [0, {}];
11+
12+
let dfs = (index) => {
13+
if (index === N + 1) {
14+
ans++;
15+
return;
16+
}
17+
18+
for (let i = 1; i <= N; i++) {
19+
if (!hash[i] && (index % i === 0 || i % index === 0)) {
20+
hash[i] = true;
21+
dfs(index + 1);
22+
hash[i] = false;
23+
}
24+
}
25+
};
26+
27+
dfs(1);
28+
return ans;
29+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /