aboutsummaryrefslogtreecommitdiff
path: root/store/works/solutions/leetcode/cpp/526.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'store/works/solutions/leetcode/cpp/526.cpp')
-rw-r--r--store/works/solutions/leetcode/cpp/526.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/store/works/solutions/leetcode/cpp/526.cpp b/store/works/solutions/leetcode/cpp/526.cpp
new file mode 100644
index 0000000..19f445f
--- /dev/null
+++ b/store/works/solutions/leetcode/cpp/526.cpp
@@ -0,0 +1,29 @@
+#include <vector>
+
+using std::vector;
+
+class Solution {
+public:
+ void dfs(int N, int index, bool *num_used, int &result) {
+ if (index > N) {
+ result += 1;
+ return;
+ }
+
+ for (int num = 1; num <= N; num++) {
+ if (!num_used[num] && (num % index == 0 || index % num == 0)) {
+ num_used[num] = true;
+ dfs(N, index + 1, num_used, result);
+ num_used[num] = false;
+ }
+ }
+ }
+
+ int countArrangement(int N) {
+ bool *num_used = new bool[N + 1]{false};
+ int result = 0;
+ dfs(N, 1, num_used, result);
+ delete[] num_used;
+ return result;
+ }
+};