From c04e8961b0c8eef7355b66ce54e42cbf8019ae61 Mon Sep 17 00:00:00 2001 From: krahets Date: Fri, 21 Jul 2023 22:21:09 +0800 Subject: [PATCH] build --- .../build_binary_tree_problem.md | 30 +++++++++++++++-- chapter_divide_and_conquer/hanota_problem.md | 32 +++++++++++++++++-- chapter_greedy/fractional_knapsack_problem.md | 6 ++-- chapter_greedy/max_capacity_problem.md | 6 ++-- chapter_greedy/max_product_cutting_problem.md | 6 ++-- chapter_hashing/hash_map.md | 4 +-- 6 files changed, 65 insertions(+), 19 deletions(-) diff --git a/chapter_divide_and_conquer/build_binary_tree_problem.md b/chapter_divide_and_conquer/build_binary_tree_problem.md index 16b137ba9..acbdd2a96 100644 --- a/chapter_divide_and_conquer/build_binary_tree_problem.md +++ b/chapter_divide_and_conquer/build_binary_tree_problem.md @@ -171,9 +171,35 @@ status: new === "Go" ```go title="build_tree.go" - [class]{}-[func]{dfs} + /* 构建二叉树:分治 */ + func dfsBuildTree(preorder, inorder []int, hmap map[int]int, i, l, r int) *TreeNode { + // 子树区间为空时终止 + if r-l < 0 { + return nil + } + // 初始化根节点 + root := NewTreeNode(preorder[i]) + // 查询 m ,从而划分左右子树 + m := hmap[preorder[i]] + // 子问题:构建左子树 + root.Left = dfsBuildTree(preorder, inorder, hmap, i+1, l, m-1) + // 子问题:构建右子树 + root.Right = dfsBuildTree(preorder, inorder, hmap, i+1+m-l, m+1, r) + // 返回根节点 + return root + } - [class]{}-[func]{buildTree} + /* 构建二叉树 */ + func buildTree(preorder, inorder []int) *TreeNode { + // 初始化哈希表,存储 inorder 元素到索引的映射 + hmap := make(map[int]int, len(inorder)) + for i := 0; i < len(inorder); i++ { + hmap[inorder[i]] = i + } + + root := dfsBuildTree(preorder, inorder, hmap, 0, 0, len(inorder)-1) + return root + } ``` === "JavaScript" diff --git a/chapter_divide_and_conquer/hanota_problem.md b/chapter_divide_and_conquer/hanota_problem.md index f34a26be9..d87b2ee80 100644 --- a/chapter_divide_and_conquer/hanota_problem.md +++ b/chapter_divide_and_conquer/hanota_problem.md @@ -193,11 +193,37 @@ status: new === "Go" ```go title="hanota.go" - [class]{}-[func]{move} + /* 移动一个圆盘 */ + func move(src, tar *list.List) { + // 从 src 顶部拿出一个圆盘 + pan := src.Back() + // 将圆盘放入 tar 顶部 + tar.PushBack(pan.Value) + // 移除 src 顶部圆盘 + src.Remove(pan) + } - [class]{}-[func]{dfs} + /* 求解汉诺塔:问题 f(i) */ + func dfsHanota(i int, src, buf, tar *list.List) { + // 若 src 只剩下一个圆盘,则直接将其移到 tar + if i == 1 { + move(src, tar) + return + } + // 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf + dfsHanota(i-1, src, tar, buf) + // 子问题 f(1) :将 src 剩余一个圆盘移到 tar + move(src, tar) + // 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar + dfsHanota(i-1, buf, src, tar) + } - [class]{}-[func]{hanota} + /* 求解汉诺塔 */ + func hanota(A, B, C *list.List) { + n := A.Len() + // 将 A 顶部 n 个圆盘借助 B 移到 C + dfsHanota(n, A, B, C) + } ``` === "JavaScript" diff --git a/chapter_greedy/fractional_knapsack_problem.md b/chapter_greedy/fractional_knapsack_problem.md index 22849898c..30da64082 100644 --- a/chapter_greedy/fractional_knapsack_problem.md +++ b/chapter_greedy/fractional_knapsack_problem.md @@ -15,8 +15,6 @@ status: new

Fig. 分数背包问题的示例数据

-### 第一步:问题分析 - 本题和 0-1 背包整体上非常相似,状态包含当前物品 $i$ 和容量 $c$ ,目标是求不超过背包容量下的最大价值。 不同点在于,本题允许只选择物品的一部分,我们可以对物品任意地进行切分,并按照重量比例来计算物品价值,因此有: @@ -28,7 +26,7 @@ status: new

Fig. 物品在单位重量下的价值

-### 第二步:贪心策略确定 +### 贪心策略确定 最大化背包内物品总价值,**本质上是要最大化单位重量下的物品价值**。由此便可推出本题的贪心策略: @@ -222,7 +220,7 @@ status: new 最差情况下,需要遍历整个物品列表,**因此时间复杂度为 $O(n)$** ,其中 $n$ 为物品数量。由于初始化了一个 `Item` 对象列表,**因此空间复杂度为 $O(n)$** 。 -### 第三步:正确性证明 +### 正确性证明 采用反证法。假设物品 $x$ 是单位价值最高的物品,使用某算法求得最大价值为 $res$ ,但该解中不包含物品 $x$ 。 diff --git a/chapter_greedy/max_capacity_problem.md b/chapter_greedy/max_capacity_problem.md index 8a51f81f7..c67d731d4 100644 --- a/chapter_greedy/max_capacity_problem.md +++ b/chapter_greedy/max_capacity_problem.md @@ -15,8 +15,6 @@ status: new

Fig. 最大容量问题的示例数据

-### 第一步:问题分析 - 容器由任意两个隔板围成,**因此本题的状态为两个隔板的索引,记为 $[i, j]$** 。 根据定义,容量等于高度乘以宽度,其中高度由短板决定,宽度是两隔板的索引之差。设容量为 $cap[i, j]$ ,可得计算公式: @@ -27,7 +25,7 @@ $$ 设数组长度为 $n$ ,两个隔板的组合数量(即状态总数)为 $C_n^2 = \frac{n(n - 1)}{2}$ 个。最直接地,**我们可以穷举所有状态**,从而求得最大容量,时间复杂度为 $O(n^2)$ 。 -### 第二步:贪心策略确定 +### 贪心策略确定 当然,这道题还有更高效率的解法。如下图所示,现选取一个状态 $[i, j]$ ,其满足索引 $i < j$ 且高度 $ht[i] < ht[j]$ ,即 $i$ 为短板、 $j$ 为长板。 @@ -208,7 +206,7 @@ $$ [class]{}-[func]{maxCapacity} ``` -### 第三步:正确性证明 +### 正确性证明 之所以贪心比穷举更快,是因为每轮的贪心选择都会“跳过”一些状态。 diff --git a/chapter_greedy/max_product_cutting_problem.md b/chapter_greedy/max_product_cutting_problem.md index e1276463d..065c476e1 100644 --- a/chapter_greedy/max_product_cutting_problem.md +++ b/chapter_greedy/max_product_cutting_problem.md @@ -9,8 +9,6 @@ status: new 给定一个正整数 $n$ ,将其切分为至少两个正整数的和,求切分后所有整数的乘积最大是多少。 -### 第一步:问题分析 - ![最大切分乘积的问题定义](max_product_cutting_problem.assets/max_product_cutting_definition.png)

Fig. 最大切分乘积的问题定义

@@ -29,7 +27,7 @@ $$ 我们需要思考的是:切分数量 $m$ 应该多大,每个 $n_i$ 应该是多少? -### 第二步:贪心策略确定 +### 贪心策略确定 根据经验,两个整数的和往往比它们的积更小。假设从 $n$ 中分出一个因子 $2$ ,则它们的乘积为 $2(n-2)$ 。我们将该乘积与 $n$ 作比较: @@ -205,7 +203,7 @@ $$ 变量 $a$ , $b$ 使用常数大小的额外空间,**因此空间复杂度为 $O(1)$** 。 -### 第三步:正确性证明 +### 正确性证明 使用反证法,只分析 $n \geq 3$ 的情况。 diff --git a/chapter_hashing/hash_map.md b/chapter_hashing/hash_map.md index 29d9f3acb..14c784c41 100755 --- a/chapter_hashing/hash_map.md +++ b/chapter_hashing/hash_map.md @@ -1410,8 +1410,8 @@ index = hash(key) % capacity 对于上述示例中的哈希函数,当输入的 `key` 后两位相同时,哈希函数的输出结果也相同。例如,查询学号为 12836 和 20336 的两个学生时,我们得到: ```shell -12386 % 100 = 36 -20386 % 100 = 36 +12836 % 100 = 36 +20336 % 100 = 36 ``` 如下图所示,两个学号指向了同一个姓名,这显然是不对的。我们将这种多个输入对应同一输出的情况称为「哈希冲突 Hash Collision」。