Skip to content

Commit 061e61a

Browse files
committed
+ problem 526
1 parent 45a5687 commit 061e61a

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 526. Beautiful Arrangement
2+
Suppose you have `n` integers labeled `1` through `n`. A permutation of those `n` integers `perm` (**1-indexed**) is considered a **beautiful arrangement** if for every `i` (`1 <= i <= n`), **either** of the following is true:
3+
4+
* `perm[i]` is divisible by `i`.
5+
* `i` is divisible by `perm[i]`.
6+
7+
Given an integer `n`, return *the **number** of the **beautiful arrangements** that you can construct*.
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> n = 2
12+
<strong>Output:</strong> 2
13+
<strong>Explanation:</strong>
14+
The first beautiful arrangement is [1,2]:
15+
- perm[1] = 1 is divisible by i = 1
16+
- perm[2] = 2 is divisible by i = 2
17+
The second beautiful arrangement is [2,1]:
18+
- perm[1] = 2 is divisible by i = 1
19+
- i = 2 is divisible by perm[2] = 1
20+
</pre>
21+
22+
#### Example 2:
23+
<pre>
24+
<strong>Input:</strong> n = 1
25+
<strong>Output:</strong> 1
26+
</pre>
27+
28+
#### Constraints:
29+
* `1 <= n <= 15`
30+
31+
## Solutions (Rust)
32+
33+
### 1. Solution
34+
```Rust
35+
impl Solution {
36+
pub fn count_arrangement(n: i32) -> i32 {
37+
Self::backtracking(n, 1, 0)
38+
}
39+
40+
fn backtracking(n: i32, i: i32, bitmask: i32) -> i32 {
41+
if i > n {
42+
return 1;
43+
}
44+
45+
(1..=n)
46+
.filter(|p| (bitmask >> p) & 1 == 0 && (p % i == 0 || i % p == 0))
47+
.map(|p| Self::backtracking(n, i + 1, bitmask | (1 << p)))
48+
.sum()
49+
}
50+
}
51+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 526. 优美的排列
2+
假设有从 1 到 n 的 n 个整数。用这些整数构造一个数组 `perm`**下标从 1 开始**),只要满足下述条件 **之一** ,该数组就是一个 **优美的排列**
3+
4+
* `perm[i]` 能够被 `i` 整除
5+
* `i` 能够被 `perm[i]` 整除
6+
7+
给你一个整数 `n` ,返回可以构造的 **优美排列****数量**
8+
9+
#### 示例 1:
10+
<pre>
11+
<strong>输入:</strong> n = 2
12+
<strong>输出:</strong> 2
13+
<strong>解释:</strong>
14+
第 1 个优美的排列是 [1,2]:
15+
- perm[1] = 1 能被 i = 1 整除
16+
- perm[2] = 2 能被 i = 2 整除
17+
第 2 个优美的排列是 [2,1]:
18+
- perm[1] = 2 能被 i = 1 整除
19+
- i = 2 能被 perm[2] = 1 整除
20+
</pre>
21+
22+
#### 示例 2:
23+
<pre>
24+
<strong>输入:</strong> n = 1
25+
<strong>输出:</strong> 1
26+
</pre>
27+
28+
#### 提示:
29+
* `1 <= n <= 15`
30+
31+
## 题解 (Rust)
32+
33+
### 1. 题解
34+
```Rust
35+
impl Solution {
36+
pub fn count_arrangement(n: i32) -> i32 {
37+
Self::backtracking(n, 1, 0)
38+
}
39+
40+
fn backtracking(n: i32, i: i32, bitmask: i32) -> i32 {
41+
if i > n {
42+
return 1;
43+
}
44+
45+
(1..=n)
46+
.filter(|p| (bitmask >> p) & 1 == 0 && (p % i == 0 || i % p == 0))
47+
.map(|p| Self::backtracking(n, i + 1, bitmask | (1 << p)))
48+
.sum()
49+
}
50+
}
51+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn count_arrangement(n: i32) -> i32 {
3+
Self::backtracking(n, 1, 0)
4+
}
5+
6+
fn backtracking(n: i32, i: i32, bitmask: i32) -> i32 {
7+
if i > n {
8+
return 1;
9+
}
10+
11+
(1..=n)
12+
.filter(|p| (bitmask >> p) & 1 == 0 && (p % i == 0 || i % p == 0))
13+
.map(|p| Self::backtracking(n, i + 1, bitmask | (1 << p)))
14+
.sum()
15+
}
16+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@
321321
[521][521l] |[Longest Uncommon Subsequence I][521] |![rb]&nbsp;&nbsp;![rs]
322322
[524][524l] |[Longest Word in Dictionary through Deleting][524] |![py]
323323
[525][525l] |[Contiguous Array][525] |![rb]&nbsp;&nbsp;![rs]
324+
[526][526l] |[Beautiful Arrangement][526] |![rs]
324325
[528][528l] |[Random Pick with Weight][528] |![rs]
325326
[529][529l] |[Minesweeper][529] |![rs]
326327
[530][530l] |[Minimum Absolute Difference in BST][530] |![py]
@@ -1667,6 +1668,7 @@
16671668
[521]:Problemset/0521-Longest%20Uncommon%20Subsequence%20I/README.md#521-longest-uncommon-subsequence-i
16681669
[524]:Problemset/0524-Longest%20Word%20in%20Dictionary%20through%20Deleting/README.md#524-longest-word-in-dictionary-through-deleting
16691670
[525]:Problemset/0525-Contiguous%20Array/README.md#525-contiguous-array
1671+
[526]:Problemset/0526-Beautiful%20Arrangement/README.md#526-beautiful-arrangement
16701672
[528]:Problemset/0528-Random%20Pick%20with%20Weight/README.md#528-random-pick-with-weight
16711673
[529]:Problemset/0529-Minesweeper/README.md#529-minesweeper
16721674
[530]:Problemset/0530-Minimum%20Absolute%20Difference%20in%20BST/README.md#530-minimum-absolute-difference-in-bst
@@ -3013,6 +3015,7 @@
30133015
[521l]:https://leetcode.com/problems/longest-uncommon-subsequence-i/
30143016
[524l]:https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/
30153017
[525l]:https://leetcode.com/problems/contiguous-array/
3018+
[526l]:https://leetcode.com/problems/beautiful-arrangement/
30163019
[528l]:https://leetcode.com/problems/random-pick-with-weight/
30173020
[529l]:https://leetcode.com/problems/minesweeper/
30183021
[530l]:https://leetcode.com/problems/minimum-absolute-difference-in-bst/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@
321321
[521][521l] |[最长特殊序列 Ⅰ][521] |![rb]&nbsp;&nbsp;![rs]
322322
[524][524l] |[通过删除字母匹配到字典里最长单词][524] |![py]
323323
[525][525l] |[连续数组][525] |![rb]&nbsp;&nbsp;![rs]
324+
[526][526l] |[优美的排列][526] |![rs]
324325
[528][528l] |[按权重随机选择][528] |![rs]
325326
[529][529l] |[扫雷游戏][529] |![rs]
326327
[530][530l] |[二叉搜索树的最小绝对差][530] |![py]
@@ -1667,6 +1668,7 @@
16671668
[521]:Problemset/0521-Longest%20Uncommon%20Subsequence%20I/README_CN.md#521-最长特殊序列-i
16681669
[524]:Problemset/0524-Longest%20Word%20in%20Dictionary%20through%20Deleting/README_CN.md#524-通过删除字母匹配到字典里最长单词
16691670
[525]:Problemset/0525-Contiguous%20Array/README_CN.md#525-连续数组
1671+
[526]:Problemset/0526-Beautiful%20Arrangement/README_CN.md#526-优美的排列
16701672
[528]:Problemset/0528-Random%20Pick%20with%20Weight/README_CN.md#528-按权重随机选择
16711673
[529]:Problemset/0529-Minesweeper/README_CN.md#529-扫雷游戏
16721674
[530]:Problemset/0530-Minimum%20Absolute%20Difference%20in%20BST/README_CN.md#530-二叉搜索树的最小绝对差
@@ -3013,6 +3015,7 @@
30133015
[521l]:https://leetcode.cn/problems/longest-uncommon-subsequence-i/
30143016
[524l]:https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting/
30153017
[525l]:https://leetcode.cn/problems/contiguous-array/
3018+
[526l]:https://leetcode.cn/problems/beautiful-arrangement/
30163019
[528l]:https://leetcode.cn/problems/random-pick-with-weight/
30173020
[529l]:https://leetcode.cn/problems/minesweeper/
30183021
[530l]:https://leetcode.cn/problems/minimum-absolute-difference-in-bst/

0 commit comments

Comments
 (0)