Skip to content

Commit 75bda6f

Browse files
committed
solve problem Beautiful Arrangement
1 parent a9f9065 commit 75bda6f

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ All solutions will be accepted!
264264
|59|[Spiral Matrix II](https://leetcode-cn.com/problems/spiral-matrix-ii/description/)|[java/py/js](./algorithms/SpiralMatrixII)|Medium|
265265
|885|[Spiral Matrix III](https://leetcode-cn.com/problems/spiral-matrix-iii/description/)|[java/py/js](./algorithms/SpiralMatrixIII)|Medium|
266266
|19|[Remove Nth Node From End Of List](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/)|[java/py/js](./algorithms/RemoveNthNodeFromEndOfList)|Medium|
267+
|526|[Beautiful Arrangement](https://leetcode-cn.com/problems/beautiful-arrangement/description/)|[java/py/js](./algorithms/BeautifulArrangement)|Medium|
267268

268269
# Database
269270
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Beautiful Arrangement
2+
We can solve this problem by Backtracking Algorithm
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int countArrangement(int N) {
3+
int[] res = new int[]{0};
4+
List<Integer> candidates = new ArrayList<Integer>();
5+
for (int i = 1; i <= N; i++) {
6+
candidates.add(i);
7+
}
8+
backTrack(candidates, 0, res);
9+
return res[0];
10+
}
11+
12+
public void backTrack(List<Integer> candidates, int i, int[] res) {
13+
int size = candidates.size();
14+
if (size == 0) {
15+
res[0]++;
16+
} else {
17+
i++;
18+
for (int j = 0; j < size; j++) {
19+
int c = candidates.get(j);
20+
if (c % i == 0 || i % c == 0) {
21+
List<Integer> tempCandidates = new ArrayList<Integer>(candidates);
22+
tempCandidates.remove(j);
23+
backTrack(tempCandidates, i, res);
24+
}
25+
}
26+
}
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number} N
3+
* @return {number}
4+
*/
5+
var countArrangement = function(N) {
6+
let res = [],
7+
candidates = []
8+
9+
for (let i = 1; i <= N; i++)
10+
candidates.push(i)
11+
12+
backTrack(candidates, [], res)
13+
return res.length
14+
};
15+
16+
var backTrack = function (candidates, temp, res) {
17+
if (candidates.length == 0) {
18+
res.push(temp)
19+
} else {
20+
let i = temp.length + 1
21+
candidates.forEach((c, index) => {
22+
if (c % i == 0 || i % c == 0) {
23+
let tempCandidates = candidates.slice(),
24+
t = temp.slice()
25+
tempCandidates.splice(index, 1)
26+
t.push(c)
27+
backTrack(tempCandidates, t, res)
28+
}
29+
})
30+
}
31+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def countArrangement(self, N):
3+
"""
4+
:type N: int
5+
:rtype: int
6+
"""
7+
res = []
8+
self.backtrack(range(1, N + 1), [], res)
9+
return len(res)
10+
11+
def backtrack(self, candidates, temp, res):
12+
if len(candidates) == 0:
13+
res.append(temp)
14+
else:
15+
i = len(temp) + 1
16+
for c in candidates:
17+
if c % i == 0 or i % c == 0:
18+
temp_candidates = candidates[:]
19+
temp_candidates.remove(c)
20+
t = temp[:]
21+
t.append(c)
22+
self.backtrack(temp_candidates, t, res)

0 commit comments

Comments
 (0)