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

Appearance settings

Commit 2b49716

Browse filesBrowse files
authored
Create 03 beautifulArrangement1.cpp
1 parent cf4c1df commit 2b49716
Copy full SHA for 2b49716

File tree

Expand file treeCollapse file tree

1 file changed

+24
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+24
-0
lines changed
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int cnt = 0;
4+
void calculate(int n, int pos, vector<bool> &visited) {
5+
if (pos > n) {
6+
cnt++;
7+
}
8+
for (int i = 1; i <= n; ++i) {
9+
if (!visited[i] && (((pos%i) == 0) || ((i%pos) == 0))) {
10+
visited[i] = true;
11+
calculate(n, pos+1, visited);
12+
visited[i] = false;
13+
}
14+
}
15+
}
16+
17+
int countArrangement(int n) {
18+
cnt = 0;
19+
vector<bool> visited (n+1, false);
20+
calculate(n, 1, visited);
21+
22+
return cnt;
23+
}
24+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.