mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
feat: Add the section of n queens problem (#483)
* Add the section of n queens problem * Update n_queens.py * Update n_queens.java * Update n_queens.cpp * Update n_queens.java
This commit is contained in:
@@ -218,3 +218,9 @@
|
||||
下图展示了两个剪枝条件的生效范围。注意,树中的每个节点代表一个选择,从根节点到叶节点的路径上的各个节点构成一个排列。
|
||||
|
||||

|
||||
|
||||
## 复杂度分析
|
||||
|
||||
假设元素两两之间互不相同,则 $n$ 个元素共有 $n!$ 种排列(阶乘);在记录结果时,需要复制长度为 $n$ 的列表,使用 $O(n)$ 时间。因此,**时间复杂度为 $O(n!n)$** 。
|
||||
|
||||
最大递归深度为 $n$ ,使用 $O(n)$ 栈帧空间。`selected` 使用 $O(n)$ 空间。同一时刻最多共有 $n$ 个 `duplicated` ,使用 $O(n^2)$ 空间。因此,**全排列 I 的空间复杂度为 $O(n)$ ,全排列 II 的空间复杂度为 $O(n^2)$** 。
|
||||
|
||||
Reference in New Issue
Block a user