diff --git a/526.cpp b/526.cpp index 01fa8fd..226aaf8 100644 --- a/526.cpp +++ b/526.cpp @@ -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& values, int l, int r, int& quantity) { + void permutate(vector& 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]); } }