Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions 526.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,25 @@ class Solution {
values.push_back(i);
}

this->permutate(values, 0, n - 1, quantity);
this->permutate(values, 0, quantity);

return quantity;
}

void permutate(vector<int>& values, int l, int r, int& quantity) {
void permutate(vector<int>& values, int l, int& quantity) {
// Get permutation
if (l == r) {
bool isBeautiful = true;

for (int i = 0; i < values.size(); i++) {
if (values[i] % (i + 1) != 0 && (i + 1) % values[i] != 0) {
isBeautiful = false;
break;
}
}

if (isBeautiful)
quantity++;
if (l == values.size()) {
quantity++;
} else {
for (int i = l; i <= r; i++) {
for (int i = l; i < values.size(); i++) {
// Swap
swap(values[l], values[i]);

// Recursion call
if (values[l] % (l + 1) == 0 || (l + 1) % values[l] == 0)
this->permutate(values, l + 1, r, quantity);
this->permutate(values, l + 1, quantity);

// Backtrack (This step is done because we want to have O(n) on space complexity)
// Backtrack
swap(values[l], values[i]);
}
}
Expand Down